【问题标题】:c# filenotfoundexception on webbrowser?c# webbrowser上的filenotfoundexception?
【发布时间】:2011-06-11 21:21:45
【问题描述】:
if (webBrowser1.DocumentText.IndexOf("Page: 1") != -1)

在上面一行我得到了这个异常

System.IO.FileNotFoundException 是 unhandled Message="系统无法 找到指定的文件。 (例外 来自 HRESULT:0x80070002)"
来源="System.Windows.Forms"
堆栈跟踪: 在 System.Windows.Forms.UnsafeNativeMethods.IPersistStreamInit.Save(IStream pstm,布尔 fClearDirty) 在 System.Windows.Forms.WebBrowser.get_DocumentStream() 在 System.Windows.Forms.WebBrowser.get_DocumentText() 在 WindowsFormsApplication1.Form1.GenerateETGWorklists() 在 C:\Documents 和 设置\阿戈登\我的文档\视觉 工作室 2008\Projects\GenerateWorklists\GenerateWorklists\Form1.cs:line 603 在 WindowsFormsApplication1.Form1.btnProcess_Click(对象 发件人,EventArgs e) 在 C:\Documents 和设置\阿戈登\我的 文档\Visual Studio 2008\Projects\GenerateWorklists\GenerateWorklists\Form1.cs:line 55 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息& 米) 在 System.Windows.Forms.ButtonBase.WndProc(消息& 米) 在 System.Windows.Forms.Button.WndProc(消息& 米) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& 米) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& 米) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr 参数) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& 味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 主窗体) 在 WindowsFormsApplication1.Program.Main() 在 C:\Documents 和 设置\阿戈登\我的文档\视觉 工作室 2008\Projects\GenerateWorklists\GenerateWorklists\Program.cs:line 18 在 System.AppDomain._nExecuteAssembly(程序集 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 汇编文件,证据 装配安全,字符串 [] 参数) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象 状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext 执行上下文,上下文回调 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:

这是什么意思?我昨天没有收到这个错误,今天收到了。网页打开没问题,文字Page: 1肯定在那里。

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

【问题讨论】:

  • WebBrowser 显示的是什么?
  • 您需要检查该文件是否仍在您的代码调用它的位置,没有其他进程正在使用该文件,并且权限仍然允许写入和读取文件
  • @daniel 还没有文件。它从网页中获取
  • @JOE,你确定你已经安装了 IE 及其所有核心文件和 dll 吗?
  • @dead 昨天工作正常

标签: c# .net browser


【解决方案1】:

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

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

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

别忘了导入mshtml作为参考

【讨论】:

    【解决方案2】:

    试试webBrowser1.Document.Body.InnerText

    【讨论】:

    • 什么消息和堆栈跟踪?
    • 昨天运行良好,网络浏览器正在显示网页
    【解决方案3】:

    当我从 web 加载页面时,web 浏览器控件抛出与文件访问相关的异常,我也觉得很奇怪。

    当我研究这个时,我注意到了一些奇怪的事情: 当临时 Internet 文件最近被清除时,此错误很少见。

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

    我用于清理临时 Internet 文件的代码如下...我认为这不适用于所有操作系统 - 可能有更好的方法 - 但这适合我的需要,因为它适用于 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
    

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 2016-08-06
      • 2013-01-11
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 2013-04-18
      • 1970-01-01
      相关资源
      最近更新 更多