66

In my app I have a WebBrowser element.

I would like to load a local file in it.

I have some questions:

  1. Where to place the HTML file (so that it will also be installed if a user executes the setup)
  2. how to reference the file? (e.g. my guess is the user's installation folder would not always be the same)

EDIT

I've added the HTML file to my project.

And I have set it up so that it gets copied to output folder.

When I check it it is present when run: \bin\Debug\Documentation\index.html

However when I do the following I get a 'Page cannot be displayed' error in the webbrowser element.

I use the following code to try to display the HTML file in the Webbrowser.

webBrowser1.Navigate(@".\Documentation\index.html");
PeeHaa
  • 71,436
  • 58
  • 190
  • 262

9 Answers9

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

Sylvain Rodrigue
  • 4,751
  • 5
  • 53
  • 67
ghostJago
  • 3,381
  • 5
  • 36
  • 51
  • 6
    means its a vebatim string. In C# by using @ you don't have to bother escaping any chars because @ means treat it as literal. You should use @ for paths or you'll need to do this **".\\my_html.html"** – ghostJago Aug 25 '11 at 18:13
  • 3
    I should mention as well that, when applied to a path, **@".\"** means the current directory – ghostJago Aug 25 '11 at 18:14
  • 1
    tnx didn't know that. However it is not working (must be something I do wrong :) ). When I do: `webBrowser1.Url = @".\Documentation\index.html";` I get a error: string could not be converted to Uri. And when I try: `webBrowser1.Url = new Uri(@".\Documentation\index.html");` I get: Invalid URI: The format of the URI could not be determined. – PeeHaa Aug 25 '11 at 18:18
  • 1
    Ok, try this `string curDir = Directory.GetCurrentDirectory();` `this.webBrowser1.Url = new Uri(String.Format("file:///{0}/my_html.html", curDir));` You'll need to edit your variables and probably the file name. I've tried this and it works – ghostJago Aug 25 '11 at 19:12
  • 1
    @ghotsJago: sweet! it is working now. Can you please update your answer with this solution? – PeeHaa Aug 25 '11 at 19:19
14

quite late but it's the first hit i found from google

Instead of using the current directory or getting the assembly, just use the Application.ExecutablePath property:

//using System.IO;  
string applicationDirectory = Path.GetDirectoryName(Application.ExecutablePath);
string myFile = Path.Combine(applicationDirectory, "Sample.html");
webMain.Url = new Uri("file:///" + myFile);
sq33G
  • 3,320
  • 1
  • 23
  • 38
mickeymicks
  • 637
  • 8
  • 13
  • 5
    Keep in mind that [Application.ExecutablePath](http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath%28v=vs.110%29.aspx) is not available on the compact framework and `Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)` must be used. – Brett Ryan Dec 16 '13 at 13:25
6

Note that the file:/// scheme does not work on the compact framework, at least it doesn't with 5.0.

You will need to use the following:

string appDir = Path.GetDirectoryName(
    Assembly.GetExecutingAssembly().GetName().CodeBase);
webBrowser1.Url = new Uri(Path.Combine(appDir, @"Documentation\index.html"));
Brett Ryan
  • 26,937
  • 30
  • 128
  • 163
4
  1. Place it in the Applications setup folder or in a separte folder beneath
  2. Reference it relative to the current directory when your app runs.
Jan
  • 15,802
  • 5
  • 35
  • 59
3
  1. Somewhere, nearby the assembly you're going to run.
  2. Use reflection to get path to your executing assembly, then do some magic to locate your HTML file.

Like this:

var myAssembly = System.Reflection.Assembly.GetEntryAssembly();
var myAssemblyLocation = System.IO.Path.GetDirectoryName(a.Location);
var myHtmlPath = Path.Combine(myAssemblyLocation, "my.html");
Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111
1

What worked for me was

<WebBrowser Source="pack://siteoforigin:,,,/StartPage.html" />

from here. I copied StartPage.html to the same output directory as the xaml-file and it loaded it from that relative path.

Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
0

Windows 10 uwp application.

Try this:

webview.Navigate(new Uri("ms-appx-web:///index.html"));
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
0

Update on @ghostJago answer above

for me it worked as the following lines in VS2017

string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Navigate(new Uri(String.Format("file:///{0}/my_html.html", curDir)));
Nouman Bhatti
  • 1,341
  • 6
  • 28
  • 54
0

I have been trying different answers from here, but managed to derive something working, here it is:
1- Added the page in a folder i created at project level named WebPagesHelper
2- To have the page printed by webBrowser Control,

string curDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);                
var uri = new Uri(curDirectory);
string myFile = Path.Combine(uri.AbsolutePath, @"WebPagesHelper\index.html");
Uri new_uri = new Uri(myFile);

i had to get the assembly path, create a first uri to get an absolute path without the 'file://' attached, next i combined this absolute path with a relative path to the page in its folder, then made another URI from the result.
Then pass this to webBrowser URL property webBrowser.URL = new_uri;

Flywings
  • 72
  • 4