So continuing on the theme of using .NET's WebBrowser control ... we need to load our first page. If we add HTML file to our solution then we can use Reflection to find where our assembly is executing and go up a couple of directories to find the HTML file and navigate to it.
Assembly myExe = System.Reflection.Assembly.GetExecutingAssembly();
string filePath = Path.GetDirectoryName(new Uri(myExe.CodeBase).LocalPath);
string myFile = System.IO.Path.Combine(filePath, @"..\..\HtmlPage1.html");
if (File.Exists(myFile))
{
this.webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
this.webBrowser1.Navigate(myFile);
}
An alternative to using the Navigate method is to open a file IO Stream and then set this as the WebBrowser control's DocumentStream property like this ...
Assembly myExe = System.Reflection.Assembly.GetExecutingAssembly();
string filePath = Path.GetDirectoryName(new Uri(myExe.CodeBase).LocalPath);
string myFile = System.IO.Path.Combine(filePath, @"..\..\HtmlPage1.html");
if (File.Exists(myFile))
{
this.webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
System.IO.Stream str = System.IO.File.Open(myFile, FileMode.Open);
//this.webBrowser1.Navigate(myFile);
this.webBrowser1.DocumentStream = str;
}
Also note is both examples that you'd best let the document completely load before doing any processing, even if it is a local file, we use event handling and capture the DocumentCompleted event.
No comments:
Post a Comment