I want to show a local html file which exists in my project:
The calling file is the HelpFile.cs
and in that the form contains the WebBrowser
A file which exists in your project, lives in a specific location in your machine. But after you distribute the program and run it on user's machine, the file will not exists in the target machine.
You may want to distribute the file or add it as resource. To solve the problem you can use either of the following solutions:
Resources.resx
Then to show the file, you can use the following methods:
To copy the file to output directory at build time:
Then the file will be copied to your output directory and you can use it this way:
var path = System.IO.Path.Combine(Application.StartupPath, "test.html");
this.webBrowser1.Navigate(path);
Please note, if the file is located under a folder in your project, for example under MyFolder
, then it will be copied into a folder with the same name in the output directory of the application:
var path = System.IO.Path.Combine(Application.StartupPath, "MyFolder", "test.html");
this.webBrowser1.Navigate(path);
You can add the file to resource file of the project. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:
Then the content of the file will be available through a string property of the Resources
. The property name will be same as the file name, for example if the file name is test.html
, the property name will be test
and You can use it this way:
this.webBrowser1.DocumentText = Properties.Resources.test;
Please note, for this solution the file doesn't need to be distributed by your project and it will be part of the resource file. However it will be part of your project file.
You can make the file as an embedded resource. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:
Then to use, you need to get the file content from embedded resources. Assuming the file name is "test.html":
var fileName = "test.html";
var name = Assembly.GetExecutingAssembly().GetManifestResourceNames()
.Where(x => x.EndsWith(fileName)).First();
webBrowser1.DocumentStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
Please note, if you have the file inside a folder like MyFolder
in the project, then the filename in above example will be "MyFolder.test.html".