2

I am trying to load a local HTML file into an instance of C# WebBrowser (WinForms). This is what I am doing:

string url  = @"file:///C:MyHtml/hello.html";
myWebbrowser.Url    = new Uri(url, UriKind.Absolute);
object test = myWebbrowser.Url; // breakpoint here

The path above is correct; if I copy it and paste into an external browser, the file is immediately opened. But the instance of WebBrowser does not want to react. I set a breakpoint in the last line of the snippet, and what I get there is that myWebbrowser.Url is null (the test variable). The control remains correspondingly empty.

myWebbrowser.AllowNavigation is explicitly set to true. I have also tried all possible versions of slashes and backslashes; the result is always the same. The version of the webbrowser seems to be 11 (myWebbrowser.Version = "{11.0.18362.1139}"). I am working in Windows 10, VS 2019.

What can be wrong in this setup?

Francesca Y
  • 183
  • 1
  • 11
  • 2
    The format is `new Uri(String.Format("file:///{0}/hello.html", Directory.GetCurrentDirectory()));` *or* `new Uri(String.Format(pathtofile, directoryfileisin));` – Trevor Oct 27 '20 at 12:48
  • Does this answer your question? [Load local HTML file in a C# WebBrowser](https://stackoverflow.com/questions/7194851/load-local-html-file-in-a-c-sharp-webbrowser) – Trevor Oct 27 '20 at 12:52
  • @Çöđěxěŕ Don't be surprised: I have just typed my comment in Francesca's computer, we are looking together. – Alex Konnen Oct 27 '20 at 13:07
  • @Çöđěxěŕ Thanks for your tips, but is still does not work. The Url value is still null. BTW, your second version does not contain a {0} and therefore seems to me to be less reasonable. The link you mention contains principally the same tricks you suggest, I looked at them hours ago... – Francesca Y Oct 27 '20 at 13:11

2 Answers2

2

The path above is correct; if I copy it and paste into an external browser, the file is immediately opened. But the instance of WebBrowser does not want to react. I set a breakpoint in the last line of the snippet, and what I get there is that myWebbrowser.Url is null.

I was able to replicate this exact issue, it's because the property of the Url doesn't get actually set until the document has actually finished loading.

To resolve this issue, you must handle the DocumentCompleted event. You can do so for example:

 string url  = @"file:///C:MyHtml/hello.html";
 myWebbrowser.DocumentCompleted += MyWebbrowser_DocumentCompleted;
 myWebbrowser.Url = new Uri(url, UriKind.Absolute);

Create a new routine to handle the DocumentCompleted event:

 private void MyWebbrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
 {
    string test = myWebbrowser.Url.ToString();
 }

You can also get the Url from the WebBrowserDocumentCompletedEventArgs:

 string testUrl = e.Url.ToString();

I am not sure exactly why when setting the Url and then checking it, it is null, I haven't found anything to explain why. My only guess is that it may be an invalid Url and or path, if navigation succeeds then that property is set.

Edit: upon looking at the source for WebBrowserDocumentCompleted, it does seem the Url property is only set in the DocumentCompleted, you can see more there.


Please note: you must register the DocumentCompleted event first before setting the Url property as when you do, it will navigate first and you will not receive the DocumentCompleted event.

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • DocumentCompleted can be a fruitful idea, I will test it right now. But I still do not uinderstand why it all functions if I take the URL string as "`http://www.google.com`" ? – Alex Konnen Oct 27 '20 at 13:50
  • After DocumentCompleted handler has worked the Url in the browser is really no longer null, but the control itself remains empty. This is what it is: – Alex Konnen Oct 27 '20 at 14:56
1

I was able to find out why it did not want to function, at least I hope so. The "hello.html" file contained calls to jquery and THREE.js, while WebBroser seems not to support the latter. Therefore I did not see any content in the control. After I threw out THREE.js and inserted most simple HTML code, it worked just OK! Now I am busy trying to bring WebBrowser to support THREE.js (there exists a skeptical opinion about this, though).

Francesca Y
  • 183
  • 1
  • 11