Share via


Form.WebBrowser - System.IO.FileNotFoundException - HRESULT: 0x80070002

Question

Thursday, October 28, 2010 10:04 PM

Hi,

I am using the event "DocumentCompleted" of a WebBrowser control to read some text when the web page is loaded.

however, time to time (not always and it happens in some computers only) I get an error "System.IO.FileNotFoundException - HRESULT: 0x80070002" when I run the method:

private

 

void MyWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

{

 

       if (MyWebBrowser.ReadyState == WebBrowserReadyState.Complete)

 

            if (MyWebBrowser.DocumentText.Contains("sometext")) //sometimes an error happens

            {

             // do something

            }

}

How can I prevent this problem?, for me it is strange that it happens only with some users and with some other no.

I have noticed that it depends on the Internet Exprorer Version that It is installed, the problem happens in Explorer 6, but it works in Explorer 7.

 

 

All replies (4)

Friday, October 29, 2010 12:30 AM

WebBrowser.DocumentText is not documented as throwing that exception (FileNotFoundException), nor is the String.Contains function.  I have to believe the line number is not the line of that If statement.  It also seems strange to get a HRESULT 0x80070002 type of error with a .net Exception.  You are talking about the ..Net WebBrowser control found in System.Windows.Forms.WebBrowser, correct?

--
Mike


Friday, October 29, 2010 10:59 PM

yeap, What I did is to use a "try - catch" block to get specific "System.IO.FileNotFoundException" and continue execution. I don't like this implementation but as you said this error is not documented, attached you will find specific exception that I got:

2010-10-28.16:37:49.55@Message=The system cannot find the file specified. (Exception from HRESULT: 0x80070002)@Source=Save@Comments=System.IO.FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
   at System.Windows.Forms.UnsafeNativeMethods.IPersistStreamInit.Save(IStream pstm, Boolean fClearDirty)
   at System.Windows.Forms.WebBrowser.get_DocumentStream()
   at System.Windows.Forms.WebBrowser.get_DocumentText()
   at iAgent.FrmLoadSAPHrs.webBrowserBoschCheckers_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e) in C:\Documents and Settings\lze1tl\My Documents\MyProject\iAgent\FrmLoadSAPHrs.cs:line 190

It is strange that this happen only when the user uses explorer 6, because exprorer 7 is working without errors.


Friday, October 29, 2010 11:33 PM

I found a link, but forgot to copy it down (sorry) that said to give the webbrowser control focus before calling DocumentText.  See if that helps you.  (that is, try calling MyWebBrowser.Focus();)

--
Mike


Sunday, December 5, 2010 11:59 PM

I ran into same issue and fixed it by not calling documenttext instead i called document.body.outerhtml.contains("something")

 

If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Or WebBrowser1.ReadyState = WebBrowserReadyState.Interactive Then
'This was just to make a call to webbrowser document
Dim doc As HtmlDocument = WebBrowser1.Document

    If WebBrowser1.Document.Body.OuterHtml.Contains("something") Then


        'do some work

    end if

end if