2

当我尝试在 VB 中打开 URL 时,出现“System.dll 中出现‘System.ComponentModel.Win32Exception’类型的第一次机会异常”错误。我尝试了多种打开网站的方法,但都返回了这个错误。我现在使用的代码是这样的

Public Class Revise

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Bitesize.Click
        Dim bitesize As String = "http://www.bbc.co.uk/bitesize/standard/"
        Process.Start(bitesize)
    End Sub
End Class

我对编程很陌生,如果这是我犯的一个愚蠢的错误,我很抱歉。

错误详情:

System.ComponentModel.Win32Exception was unhandled
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Unknown error (0x80041002)
  NativeErrorCode=-2147217406
  Source=System
  StackTrace:
       at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start()
       at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start(String fileName)
       at WindowsApplication1.Revise.Button1_Click(Object sender, EventArgs e) in C:\Users\Lewis\Documents\Visual Studio 11\Projects\Study Time!\Study Time!\Revise.vb:line 7
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
4

2 回答 2

0

Process.Start 实际上是在操作系统中执行一个程序。您尝试访问的 url 不是操作系统上程序的名称,因此它会失败。如果要使用此 url 打开外部浏览器实例,则需要将该程序作为进程打开。

Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles Bitesize.Click
    Dim bitesize as String = "http://www.bbc.co.uk/bitesize/standard/"
    Dim programName as String = "iexplore.exe"

    Process.Start(programName & " " & bitesize)
End Sub

这应该可以实现您正在寻找的效果,但是在 winforms 应用程序中,使用 WebBrowser 控件并在按钮单击时填充其 url 属性会更有意义。

编辑:
要在系统的默认浏览器中打开一个 url,你可以简单地将你的 programName 设置为“start”。

Dim programName as String = "start"

这是 Windows 中的一个快捷方式,它将使用默认浏览器自动打开一个 url。

于 2012-12-07T17:15:58.750 回答
0

该代码(听起来您已经知道)将使用您计算机的默认 Web 浏览器打开该网站。您的代码是正确的(尽管方法名称应该与它正在侦听的控件匹配,因此当您有一个包含大量事件处理程序的表单时,它更容易阅读)。

我的猜测(没有详细信息 - 将来,请尝试发布异常堆栈跟踪),将无法确定您的默认 Web 浏览器。试试这个:打开控制面板\程序\默认程序\设置关联..并找到HTTP协议(在所有文件扩展名关联之后)并重置关联的应用程序。

其次,这是另一个很好的学习机会,当您的代码可能出错时(例如启动另一个进程),将代码包装在 Try/Catch 块中是一个非常好的主意。如果出现问题错了,你可以处理它,而不是让你的应用程序崩溃。

Try
    Dim bitesize As String = "http://www.bbc.co.uk/bitesize/standard/"
    Process.Start(bitesize)
Catch Ex As Exception
    MessageBox.Show(Ex.Message)
End Try

System.Diagnostic.Process.Start 的 MSDN http://msdn.microsoft.com/en-us/library/53ezey2s.aspx

如果您仍然遇到问题,请发布堆栈跟踪,这可能有助于更清楚地了解发生了什么问题。

于 2012-12-07T17:31:10.437 回答