Load Local HTML File in a C# Webbrowser

Load local HTML file in a C# WebBrowser

  1. Do a right click->properties on the file in Visual Studio.
  2. Set the Copy to Output Directory to Copy always.

Then you will be able to reference your files by using a path such as @".\my_html.html"

Copy to Output Directory will put the file in the same folder as your binary dlls when the project is built. This works with any content file, even if its in a sub folder.

If you use a sub folder, that too will be copied in to the bin folder so your path would then be @".\my_subfolder\my_html.html"

In order to create a URI you can use locally (instead of served via the web), you'll need to use the file protocol, using the base directory of your binary - note: this will only work if you set the Copy to Ouptut Directory as above or the path will not be correct.

This is what you need:

string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/my_html.html", curDir));

You'll have to change the variables and names of course.

Show an HTML file of my Project in WebBrowser control at run-time

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:

  • You can copy the file to output directory at build time
  • You can add the file to a resource file like Resources.resx
  • You can make the file as an embedded resource

Then to show the file, you can use the following methods:

  • Get the file path and call the Navigate method or assign it to Url property
  • Get the resource content and assign it to DocumentText property
  • Get the resource stream and assign it to DocumentStream property

Copy the file to Output Directory

To copy the file to output directory at build time:

  • Solution explorer → See properties of your file
  • Set Build Action to Content.
  • Set Copy to Output Directory to Copy always.

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);

Add the file to a resx resource file like Resources.Resx

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:

  • Solution explorer → Your project → Properties folder → open Resources.Resx file
  • From toolbar of the designer → Add existing file → Add the html file.

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.

Make the file as an embedded resource

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:

  • Solution explorer → See properties of your file
  • Set Build Action to Embedded Resource.
  • Set Copy to Output Directory to Do not copy.

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".

Load html file in a C# WebBrowser component

If you have a file listed in Visual Studios solution explorer, you should be able to right click it, select properties and in the properties pane set the "Copy to Output Directory" to "Copy always".

The file will then end up next to the exe in the same debug folder and you will be able to refer to it with your existing code.

Load local html file in webbrowser control in editable mode

Please check this snippet.

 webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.Url = new Uri(@"D:\TEMP\sample.htm");

c# - How to open a local html file in a web browser that is not the default one?

If you want to open a website in chrome from c#, try:

System.Diagnostics.Process.Start("chrome", "www.google.com");

Using your code, I think you're trying to get any open browser first?

var runningProcess = System.Diagnostics.Process.GetProcessesByName("chrome");
if (runningProcess.Length != 0)
{
System.Diagnostics.Process.Start("chrome", filename);
}
runningProcess = System.Diagnostics.Process.GetProcessesByName("firefox");
if (runningProcess.Length != 0)
{
System.Diagnostics.Process.Start("firefox", filename);
}
runningProcess = System.Diagnostics.Process.GetProcessesByName("iexplore");
if (runningProcess.Length != 0)
{
System.Diagnostics.Process.Start("iexplore", filename);
}

Opening a local Html file within a C# Winforms file

I think the problem is, that the WebBrowser component does not understand that relative path.

So my guess is, you have to add the full path parameter, like this:

WebReadmeText.Navigate(@"C:\Users\Keith\source\repos\Triangle\ReadmePages\Readme1.html");

In your case, this could work too, and you dont need to hard code the full path:

var baseDir = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
WebReadmeText.Navigate($@"{baseDir}\ReadmePages\readme1.html");


Related Topics



Leave a reply



Submit