Excuse me For This Novice Question.
I am using visual studio ultimate in win7.
Currently Trying to read XML file. My xml file location is C:\Users\Sael\Documents\Visual Studio 2013\Projects\Hotel_Management\Debug\temp\app.xml.
But when I am debugging my app.exe I got this message 'The file C:\Users\Sael\Documents\Visual Studio 2013\Projects\Hotel_Management\Debug\temp\app.xml was not found'
Actually my xml file is exists. Can anyone help?
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Xml;
......
XmlDocument ^ docVideo = gcnew XmlDocument;
String ^ strFilename = L"C:\Users\Sael\Documents\Visual Studio 2013\Projects\Hotel_Management\Debug\temp\app.xml";
if (File::Exists(strFilename))
{
char *path = NULL;
size_t size;
path = getcwd(path, size);
MessageBox(NULL, pwd, pwd, 0);
docVideo->Load(strFilename);
XmlElement ^ elm = docVideo->DocumentElement;
XmlNodeList ^ lstVideos = elm->ChildNodes;
MessageBox::Show("The root element contains " +
lstVideos->Count + L" nodes");
//txtDocument->Text = elm->InnerText;
}
else
MessageBox::Show(L"The file " + strFilename + L" was not found");
Related
I want to use C++/CLR to create a UI for my app. I need C++/CLR because I deal with a lot of native win32 C++ code. So I tried to make my app with the following steps, but they don't work:
Make an empty project
Unicode charset and enable /clr
Add refrences to PresentationCore, PresentationFramework, System, and WindowsBase
Add the App.xaml, App.xaml.h, MainWindow.xaml, MainWindow.xaml.h
Add the InitalizeComponent code to MainWindow.xaml.h (that
s where i'm having trouble.)
This is my mainwindow.xaml.h code
#pragma once
using namespace System::Windows;
using namespace System;
namespace Project2
{
public ref class MainWindow : Window
{
public:
MainWindow()
{
System::Uri ^resourceLocater = gcnew System::Uri("component\MainWindow.xaml", System::UriKind::Relative);
#line 1 "..\..\MainWindow.xaml"
System::Windows::Application::LoadComponent(this, resourceLocater);
}
};
}
My entry point code:
#include <Windows.h>
#include "App.xaml.h"
#include "MainWindow.xaml.h"
using namespace System;
using namespace System::Windows;
[STAThreadAttribute]
int WINAPI WinMain(HINSTANCE a, HINSTANCE b, LPSTR c, int d)
{
auto win = gcnew Project2::MainWindow();
auto app = gcnew Project2::App();
app->Run(win);
return 0;
}
And here's my app.xaml.h code:
#pragma once
using namespace System::Windows;
namespace Project2
{
public ref class App : Application
{
public:
};
}
The MainWindow.xaml and App.xaml all contain valid markup.
I tried chaning the values for the System::Uri resoucelocator but nothings chaning. I just get System.IO.IOException: The mainwindow.xaml was not found
Any ideas?
I have searched for entire day for a solution, but somehow I couldn't find it.
What I wanted is to get data from database (binary file such as PDF) into MemoryStream (which I succeeded to do so), but problem is when I wish to preview that file into WebBrowser control.
so the code goes:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace MemoryStreamTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MemoryStream memory = new MemoryStream();
//byte[] temp;
private void Form1_Load(object sender, EventArgs e)
{
memory = GetBlobFile();
}
private MemoryStream GetBlobFile()
{
MemoryStream ms = new MemoryStream();
string SQL = "SELECT [SnapshotPDF] FROM [DBtest].[dbo].[SamplePDF] WHERE id = " + 21;
SqlConnection conn = new SqlConnection("Data Source=database;Initial Catalog=DBtest; User ID=test; Password=test;");
SqlCommand comm = new SqlCommand(SQL, conn);
conn.Open();
byte [] result = (byte[])comm.ExecuteScalar();
conn.Close();
if (result != null)
{
ms.Write(result, 0, result.Length);
ms.Position = 0;
}
return ms;
}
private void button1_Click(object sender, EventArgs e)
{
//webBrowser1.DocumentText = "application/pdf";
webBrowser1.DocumentStream = memory;
}
}
}
Actually webBrowser really output content from memory, but obviously from picture above it's rendered as text...
How can I force to render as PDF?
If it is not possible to use webBrowser control, is there any other controls that I can use in WinForms that would preview/render PDF from memory.
You have to implement an async pluggable protocol, e.g. IClassFactory, IInternetProtocol... Then you use CoInternetGetSession to register your protocol. When IE calls your implementation, you can serve your image data from memory/provide mime type.
It's a bit tedious, but doable. Look at IInternetProtocol and pluggable protocols documentation on MSDN.
Done, I found one little bug which I correct it. this is entire code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Ghostscript.NET;
using Ghostscript.NET.Viewer;
namespace MemoryStreamTest
{
public partial class Form1 : Form
{
Stream stream;
byte[] result;
private GhostscriptViewer _viewer;
private GhostscriptVersionInfo _gsVersion = GhostscriptVersionInfo.GetLastInstalledVersion();
private Bitmap _pdfPage = null;
public Form1()
{
InitializeComponent();
pictureBox1.Width = 100;
pictureBox1.Height = 100;
_viewer = new GhostscriptViewer();
_viewer.DisplaySize += new GhostscriptViewerViewEventHandler(_viewer_DisplaySize);
_viewer.DisplayUpdate += new GhostscriptViewerViewEventHandler(_viewer_DisplayUpdate);
_viewer.DisplayPage += new GhostscriptViewerViewEventHandler(_viewer_DisplayPage);
}
private void Form1_Load(object sender, EventArgs e)
{
GetBlobFile();
}
private void GetBlobFile()
{
string SQL = "SELECT [SnapshotPDF] FROM [test].[dbo].[InvoiceAdded] WHERE id = " + 21;
SqlConnection conn = new SqlConnection("Data Source=test;Initial Catalog=test; User ID=test; Password=test;");
SqlCommand comm = new SqlCommand(SQL, conn);
conn.Open();
result = (byte[])comm.ExecuteScalar();
conn.Close();
stream = new MemoryStream(result);
}
private void button1_Click(object sender, EventArgs e)
{
ConvertToBitmap();
}
private void ConvertToBitmap()
{
_viewer.Open(stream, _gsVersion, true);
}
//DisplayPage
void _viewer_DisplayPage(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
}
//DisplaySize - dynamically
void _viewer_DisplaySize(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Image = e.Image;
}
//DisplayUpdate - automatic update picture
void _viewer_DisplayUpdate(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
}
}
}
keep in mind that for this I needed to add .dll from Ghostscript.NET (a nice wrapper made by my patriot from Croatia), also I needed to install Ghostscript interpreter from GhostScript interpreter
And also many thanks to user: sm.abdullah
which actually point me to right direction.
Cheers
I have to log Exception in database, I've created pprocedure, configured Database Trace Listener, but I can't understand, how to send parameters to stored procedure in database, what esle should be configured?
/*
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Data.OleDb;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
*/
public int DeleteSomething(System.Guid userUUID)
{
int rowsAffected = 0;
try
{
Database db = this.GetDatabase();
DbCommand dbc = db.GetStoredProcCommand("dbo.uspDeleteSomething");
dbc.CommandTimeout = 600;
db.AddInParameter(dbc, "#UserUUID", System.Data.DbType.Guid, userUUID );
rowsAffected = db.ExecuteNonQuery(dbc);
}
finally
{ }
return rowsAffected;
}
protected Microsoft.Practices.EnterpriseLibrary.Data.Database GetDatabase()
{
Database returnDb;
returnDb = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase();
return returnDb;
}
i could run the How To Play a File code sample in c++ win32 console application, but when i try to implement it by winforms i get these Link errors:
Error 2 error LNK2020: unresolved token (0A000016) IID_IMediaEvent
Error 3 error LNK2020: unresolved token (0A000017) IID_IMediaControl
and some more link errors .....
here is the code of the form:
#include <dshow.h>
#pragma comment(lib, "Strmiids.lib")
namespace Form1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(L"C:\\Example.avi", NULL);
if (SUCCEEDED(hr))
{
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
}
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
}
};
}
How can i setup the build environment in winforms to do the DirectShow programming?
im using windows SDK v7.1 And vc++ 2010
You are not getting a great diagnostic. The problem is that DirectShow is native code. But you are letting the compiler think it is okay to compile it in managed mode. Which works surprisingly well, until the linker takes a nosedive. You need to make it look like this:
#pragma once
#pragma managed(push, off)
#include <dshow.h>
#pragma managed(pop)
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "ole32.lib")
// etc..
This probably generates a flurry of errors. Right-click the project in the Solution Explorer window, Properties, Configuration Properties, General. Change Common Language Runtime support from /clr:pure to /clr. This played a sample .avi file properly when I tried it. In a DirectShow window, not the form. The sample code was designed only to work in a console application. You should also remove the calls to CoInitialize and CoUninitialize, .NET already initializes COM. Improving the error handling is advisable. Consider embedding Windows Media Player instead.
This is a winforms application.
In windows, I want the user to click and button, and then a popup should make the user select the path of where they want to save the file.
You want the WriteAllText function.
using (SaveFileDialog dialog = new SaveFileDialog()) {
if (dialog.ShowDialog(this) == DialogResult.OK) {
File.WriteAllText(dialog.FileName, yourStringBuilder.ToString());
}
}
Think no longer...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2 {
public partial class Form1 : Form {
StringBuilder sb = new StringBuilder();
public Form1() {
InitializeComponent();
sb.Append("This is going ");
sb.Append("to be saved to a text file");
}
private void button1_Click(object sender, EventArgs e) {
using (SaveFileDialog dlg = new SaveFileDialog()) {
if (dlg.ShowDialog() == DialogResult.OK) {
string fileName = dlg.FileName;
SaveToFile(fileName);
}
}
}
private void SaveToFile(string fileName) {
System.IO.TextWriter w = new System.IO.StreamWriter(fileName);
w.Write(sb.ToString());
w.Flush();
w.Close();
}
}
StringBuilder.ToString() will get you the string.
This link will show you how to write text to a file.
This link will show you how to call SaveFileDialog and pass a stream to it to save.
Hope that helps.
StringBuilder.ToString() can be passed to the TextStream.Write() method after creating the file.
Using the SaveFileDialog class, you can let the user select path and file name - in a standard way. Detailed examples in the doc.