webbrowser上的c#filenotfoundexception?

if (webBrowser1.DocumentText.IndexOf("Page: 1") != -1) 

在上面这行我得到了这个例外

System.IO.FileNotFoundException未处理Message =“系统找不到指定的文件。(HRESULTexception:0x80070002)”
来源= “System.Windows.Forms的”
StackTrace:位于Windows.msApplication1.Form1的System.Windows.Forms.WebBrowser.get_DocumentText()上的System.Windows.Forms.WebBrowser.get_DocumentStream()处的System.Windows.Forms.UnsafeNativeMethods.IPersistStreamInit.Save(IStream pstm,Boolean fClearDirty)。生成C:\ Documents and Settings \ agordon \ My Documents \ Visual Studio 2008 \ Projects \ GenerateWorklists \ GenerateWorklists \ Form1.cs中的ETGWorklists():位于C:\ Documents中的WindowsFormsApplication1.Form1.btnProcess_Click(Object sender,EventArgs e)的第603行和Settings \ agordon \ My Documents \ Visual Studio 2008 \ Projects \ GenerateWorklists \ GenerateWorklists \ Form1.cs:System.Windows.Forms.Button.OnClick上的System.Windows.Forms.Control.OnClick(EventArgs e)第55行(EventArgs) e)在System.Windows.Forms.Connd.WndProc(Message&m)的System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons按钮,Int32单击)处的System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)处。 System.W上的System.Windows.Forms.ButtonBase.WndProc(Message&m) 在System.Windows.Forms的System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)的System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中的indows.Forms.Button.WndProc(Message&m)。在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager的System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)的NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)的System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)中的.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData)在System.Windows.Forms.Application.Run(Form mainForm)中的WindowsFormsApplication1.Program.Main()在C:\ Documents and Settings \ agordon \ My Documents \ Visual Studio 2008 \ Projects \ GenerateWorklists \ GenerateWorklists \ Program.cs:line 1 System.Threading上的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()处的System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)上的System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)中的8 System.Threading.ThreadHelper.ThreadStart()中的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)中的.ThreadHelper.ThreadStart_Context(Object state):InnerException:

这是什么意思? 我昨天没有得到这个错误,今天得到它。 网页打开没有问题,文本Page: 1肯定存在。

这是一个类似的问题,也没有解决方案http://bytes.com/topic/c-sharp/answers/657812-webbrowser-documenttext-getting-problem

这是一个已知的错误,微软至少没有为vs2008做任何事情。 这是一个修复:

 String lastsource = ((mshtml.HTMLDocumentClass)(((webBrowser1.Document.DomDocument)))).documentElement.innerHTML; webBrowser1.Document.OpenNew(true); webBrowser1.Document.Write(lastsource); 

现在我们可以毫无问题地访问DocumentText

别忘了导入mshtml作为参考

尝试webBrowser1.Document.Body.InnerText

当我从网上加载页面时,我也发现Web浏览器控件抛出了与文件访问相关的exception,这一点也很奇怪。

在研究这个时,我注意到一些奇怪的事情:当最近清除临时互联网文件时,这个错误就不常见了。

我修改了我的应用程序,以便在应用程序启动时自动清除临时Internet文件,这足以解决90%的错误。

我的清理临时互联网文件的代码如下……我认为这不适用于所有操作系统 – 可能有更好的方法 – 但这符合我的需求,因为它适用于Server 2008。

(我的代码在vb.net中,但c#版本不应该太难理解。)

  'start cleaning the temporary internet files Dim clearFilesProcess As Process = System.Diagnostics.Process.Start("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 255") 'wait for the temporary internet files to be deleted While Not (clearFilesProcess.HasExited) System.Threading.Thread.Sleep(200) End While