I have a CHM file and a help menu and I want to add that file to resources but when I added it to resources it does not work.
I tried to add to subfolder in Resources but still no use
void HelpToolStripMenuItemClick(object sender, EventArgs e)
{
string filePath = Directory.GetCurrentDirectory() + "\\Help\\abc.chm";
try
{
//Check if already exists before making
if (!File.Exists(filePath))
{
var data = Properties.Resources.abc.chm;
using (var stream = new FileStream("abc.chm", FileMode.Create))
{
stream.Write(data, 0, data.Count());
stream.Flush();
}
MessageBox.Show("file made");
}
}
catch
{
//May already be opened
}
Help.ShowHelp(this, filePath);
}
I want to work even when this to work even when the setup is installed
on any computer
I would be better if any tells how to embedded in my setup
First of all, add the help file to your project and open the Properties window for that file. In the CopyToOutputDirectory, choose ‘Copy always’ or ‘Copy if newer’.
This will make sure, that when you’re debugging/testing your application, that it will copy the file to the bin folder.
Start your project in Debug mode and check your bin/debug output folder. Do the same for Release mode and output folder. The CHM should reside there and gets included in your deployment.
A sample code snippet for calling CHM's:
private const string sHTMLHelpFileName = "CHM-example.chm";
...
private void button1_Click(object sender, EventArgs e) {
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath + #"\" + sHTMLHelpFileName);
}
For download I provide a C# VS2008 Project including the code above and the help files with different help viewer windows (different CHM files for show case only).
Related
I was wondering if it was possible to download a file to a specific location then run it.
In this code I try to run from resource. But you may first download to your specific folder
then run from that path
In your application project, go to Properties/Resources.
Click on Add Resource.
Select "Add Existing File.."
Browse to the .exe you want to embed, select it and click "Open".
If you want to change the resource name:
In the Resource Editor, right-click the exe you added and select "rename" and enter an appropriate name.
In this example, I called it "MyTestExe".
Where you want to extract and run the executable, add code like this:
private void button_Click(object sender, EventArgs e)
{
byte[] exeBytes = Properties.Resources.MyTestExe;
string exeToRun = #"C:\TEST\MyTestExe.exe";
using (FileStream exeFile = new FileStream(exeToRun, FileMode.CreateNew))
{
exeFile.Write(exeBytes, 0, exeBytes.Length);
}
using (Process exeProcess = Process.Start(exeToRun))
{
exeProcess.WaitForExit();
}
}
I have added .chm file to my application root. when i fetch the file using below code it is referencing the path to bin/release/somehting.chm
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath+"\\"+"somehting.chm");
i want to get the path relative to installation location of application. please help.
the chm file added to the root directory is not loading after deploying the application. its not even loading while debugging in visual studio and not giving any error.
As I can see the first code snippet from your question calling Help.ShowHelp isn't so bad. Sometimes I'm using the related code below. Many solutions are possible ...
Please note, typos e.g. somehting.chm are disturbing in code snippets.
private const string sHTMLHelpFileName = "CHM-example.chm";
...
private void button1_Click(object sender, EventArgs e) {
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath + #"\" + sHTMLHelpFileName);
}
So, please open Visual Studio - Solution Explorer and check the properties of your CHM file. Go to the dropdown box shown in the snapshot below and set "Always copy" (here only German). Start your project in Debug mode and check your bin/debug output folder. Do the same for Release mode and output folder. The CHM should reside there and I hope your CHM call works.
You need :
String exeDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
So :
String HelpFilepath = "file://" + Path.Combine(exeDirectory , "somehting.chm");
Help.ShowHelp(this, path);
Answer from similar topic is:
// get full path to your startup EXE
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
// get directory of your EXE file
string exeDir = Path.GetDirectoryName(exeFile);
// and open Help
System.Windows.Forms.Help.ShowHelp(this, exeDir+"\\"+"somehting.chm");
In WPF application where I have included some files in resources, I want to execute them on a button click. How do I specify a path in Process.Start().
private void button1_Click_2(object sender, RoutedEventArgs e)
{
Process.Start("test.txt");
}
Or is there any other way?
private void button1_Click_2(object sender, RoutedEventArgs e)
{
string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\test.txt";
if (File.Exists(path))
{
Process.Start(new ProcessStartInfo(path));
}
else
{
MessageBox.Show("No file found"+path);
}
I added a message box and it showed No files found. :(
EDIT:
I Tried to check the path after publishing and this what i got.
No File Found With a Path - C:\Users\Administrator\AppData\Local\Apps\2.0... test.txt
Before I published the Application I got a path which id
No File Found at ..project..\bin\Debug\test.txt which is obvious since my Resource file not included there its Under a Resource Folder and not Debug when i add a test file in debug it open without any problem.
Can someone Help throwing some light on this case.
EDIT:
I want to open a file from Resource directory # C:\Users\Administrator\Documents\Visual Studio 2010\Projects\FastFix\FastFix\Resources Which would be included in my project when i am going to publish it is going to run as a standalone application without installation.
use this
string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\test.txt";
if (File.Exists(path))
{
Process.Start(new ProcessStartInfo(path));
}
I have a PDF file that I have imported in as a resource into my project. The file is a help document so I want to be able to include it with every deployment. I want to be able to open this file at the click of a button.
I have set the build action to "Embedd Resource". So now I want to be able to open it. However, When I try accessing the resource - My.Resources.HelpFile - it is a byte array. How would I go about opening this if I know that the end-user has a program suitable to opening PDF documents?
If I missed a previous question please point me to the right direction. I have found several questions about opening a PDF within an application, but I don't care if Adobe Reader opens seperately.
Check this out easy to open pdf file from resource.
private void btnHelp_Click(object sender, EventArgs e)
{
String openPDFFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + #"\HelpDoc.pdf";//PDF DOc name
System.IO.File.WriteAllBytes(openPDFFile, global::ProjectName.Properties.Resources.resourcePdfFileName);//the resource automatically creates
System.Diagnostics.Process.Start(openPDFFile);
}
Create a new Process:
string path = Path.Combine(Directory.GetCurrentDirectory(), "PDF-FILE.pdf");
Process P = new Process {
StartInfo = {FileName = "AcroRd32.exe", Arguments = path}
};
P.Start();
In order for this to work, the Visual Studio setting Copy to Output Directory has to be set to Copy Always for the PDF file.
If the only point of the PDF is to be opened by a PDF reader, don't embed it as a resource. Instead, have your installation copy it to a reasonable place (you could put it where the EXE is located), and run it from there. No point in copying it over and over again.
"ReferenceGuide" is the name of the pdf file that i added to my resources.
using System.IO;
using System.Diagnostics;
private void OpenPdfButtonClick(object sender, EventArgs e)
{
//Convert The resource Data into Byte[]
byte[] PDF = Properties.Resources.ReferenceGuide;
MemoryStream ms = new MemoryStream(PDF);
//Create PDF File From Binary of resources folders helpFile.pdf
FileStream f = new FileStream("helpFile.pdf", FileMode.OpenOrCreate);
//Write Bytes into Our Created helpFile.pdf
ms.WriteTo(f);
f.Close();
ms.Close();
// Finally Show the Created PDF from resources
Process.Start("helpFile.pdf");
}
File.Create("temp path");
File.WriteAllBytes("temp path", Resource.PDFFile)
You need to convert the resource into format acceptable for the program supposed to consume your file.
One way of doing this is to write the content of the resource to a file (a temp file) and then launch the program pointing it to the file.
Whether it is possible to feed the resource directly into the program depends on the program. I am not sure if it can be done with the Adobe Reader.
To write the resource content to a file you can create an instance of the MemoryStream class and pass your byte array to its constructor
This should help - I use this code frequently to open various executable, documents, etc... which I have embedded as a resource.
private void button1_Click(object sender, EventArgs e)
{
string openPDFfile = #"c:\temp\pdfName.pdf";
ExtractResource("WindowsFormsApplication1.pdfName.pdf", openPDFfile);
Process.Start(openPDFfile);
}
void ExtractResource( string resource, string path )
{
Stream stream = GetType().Assembly.GetManifestResourceStream( resource );
byte[] bytes = new byte[(int)stream.Length];
stream.Read( bytes, 0, bytes.Length );
File.WriteAllBytes( path, bytes );
}
//create a temporal file
string file = Path.GetTempFileName() + ".pdf";
//write to file
File.WriteAllBytes(file, Properties.Resources.PDF_DOCUMENT);
//open with default viewer
System.Diagnostics.Process.Start(file);
My winform application periodically pulls a flash file from an SQL database, writes it to a temporary folder, displays it in a webbroswer control, and then deletes it from the temporary folder. My application creates the temporary folder if it does not exist. The temporary folder is located in 'System.Environment.CurrentDirectory'.
My problem is the permissions of the temporary folder frequently become read only and then my application cannot delete the file. Sometimes the problem occurs immediately, and sometimes I can run the application several times before it occurs.
How do I insure that the file is deleted?
I added code to delete the temporary folder and then re-create it each time it writes to it, but this did not resolve my problem.
Only my application needs to access this folder, and the folder only holds these flash images.
I thought about using the generic 'temp' folder, but read somewhere that that could lead to problems.
Also, I got the same problem when I located the temporary folder at 'D:\'.
I'm using VS2008 on Windows XP. The application is to run on XP, Vista and 7.
Here is code.
DataSet dsFlashQuizRandom = new DataSet();
dsFlashQuizRandom = objUserDAO.GetFlashQuizRandom(intAge);
if (dsFlashQuizRandom.Tables[0].Rows[0]["large_image_blob"] != null && dsFlashQuizRandom.Tables[0].Rows[0]["file_name"].ToString().Trim() != string.Empty)
{
byte[] b = (byte[])dsFlashQuizRandom.Tables[0].Rows[0]["large_image_blob"];
if (b != null)
{
string flashFileName = dsFlashQuizRandom.Tables[0].Rows[0]["file_name"].ToString().Trim();
string targetPath = System.Environment.CurrentDirectory.ToString() + #"\images\";
string strFileName = targetPath + flashFileName;
//Delete the current version of the folder (if it exists); then create a new version of it.
if (System.IO.Directory.Exists(targetPath))
System.IO.Directory.Delete(targetPath, true);
if (!System.IO.Directory.Exists(targetPath))
System.IO.Directory.CreateDirectory(targetPath);
//Write the file to a FileStream, assign that stream to the webbrowser control.
FileStream fs = new FileStream(strFileName, FileMode.CreateNew, FileAccess.Write);
fs.Write(b, 0, b.Length);
fs.Close();
webBrowserQuizFlash.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserQuizFlash_DocumentCompleted);
webBrowserQuizFlash.Url = new System.Uri(strFileName, System.UriKind.Absolute);
}
}
//Delete the Flash Webbrowser file once it has completed loading.
private void webBrowserQuizFlash_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
FileInfo fi = new FileInfo(strFileName);
try
{
fi.Delete();
}
catch (IOException ex)
{
MessageBox.Show("IOException = " + ex); //test code
}
}
Any suggestions or a point in the right direction would be appreciated.
Cheers,
Frederick
PS--When copying my code to this post I see the color of the text is all red after the #"\images\"; Is there a problem with this part of my code, or is this a display artifact? Should I use this instead: #"\images\\";
You could use System.IO.Path.GetTempPath() to get a temp folder to use.
I assume that you're having problems with the delete due to the file being locked by some process. You might be able to get around that by using the MoveFileEx function to delete it at next reboot.
I think the accessing problem comes from another application that locks the file. One common application group that does such things would be the on access scanner from your anti-virus program.
To get a deeper look into, who accesses your file you should take a deeper look with Process Monitor to find out who will block your file.
Also you can maybe make a little change to your code:
//Write the file to a FileStream, assign that stream to the webbrowser control.
using(FileStream fs = new FileStream(strFileName, FileMode.CreateNew, FileAccess.Write))
{
fs.Write(b, 0, b.Length);
}