0

I m using c# WebBrowser control. I use following code inorder to navigate.

string FilePath = @"C:\Abc.mht";

webBrowser.Navigated += new WebBrowserNavigatedEventHandler(webBrowser_Navigated);
WebBrowser.Navigate(FilePath);

void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {

        try
        {
            webBrowser.Refresh();

            webBrowser.Navigated -= new
            WebBrowserNavigatedEventHandler(webBrowser_Navigated);
        }
        catch(Exception Ex)
        {

        }
    }

When i navigate to url "C:\Abc.mht" for first time then it gives page cannot be displayed error. But now if i navigate to some other url and then again navigate to "C:\Abc.mht" then it properly navigates.

My question is why webBrowser control shows "Page cannot be displayed" error when i navigate to url "C:\Abc.mht" for first time.

I also tried refreshing webBrowser control but still it gives "page cannot be displayed" error. Can anyone please tell me how to solve this problem?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mohit Shah
  • 152
  • 3
  • 13
  • 4
    Aaaargh. Empty `catch` clause. – Uwe Keim Nov 08 '13 at 07:39
  • The `WebBrowser` control has a set of _particular behaviors_. I have recently encountered some. All I can say is that you should try different loading techniques. Try loading a page by [using a `FileStream`](http://stackoverflow.com/questions/11560639/how-to-load-local-html-pages-in-webbrowser-control-in-c-sharp) or [setting the `URL` property](http://stackoverflow.com/questions/7194851/load-local-html-file-in-a-c-sharp-webbrowser). – Andrei V Nov 08 '13 at 07:59
  • Also, have a look at a relatively [similar problem](http://stackoverflow.com/questions/4705139/how-to-load-mht-from-stream-string-into-a-webbrowser-control) related to `mht` files. If that does't help, try using a simple `HTML` file to see if the source of the problem is the page or the control. – Andrei V Nov 08 '13 at 08:00

1 Answers1

0

I have tried your code it works perfectly so you need to change as following:

    string FilePath = @"D:\Abc.mht";
    public Form1()
    {
        InitializeComponent();
        webBrowser.Navigated += new WebBrowserNavigatedEventHandler(webBrowser_Navigated);
        webBrowser.Navigate(FilePath);
    }
    void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        try
        {
            //webBrowser.Refresh();
            webBrowser.Navigated -= new WebBrowserNavigatedEventHandler(webBrowser_Navigated);
        }
        catch (Exception Ex)
        {

        }
    }
Vaibhav Parmar
  • 645
  • 4
  • 11
  • Hi Vaibhav, I have already tried commenting WebBrowser.Refresh() from webBrowser_Navigated event. It does not work. – Mohit Shah Nov 08 '13 at 10:50
  • But it works on my side the Abc.mht file is loaded completed can you send me your code on vparmar@ismnet.com so i can solve it completely. – Vaibhav Parmar Nov 08 '13 at 10:55