我在VB中尝试打开URL时遇到错误

时间:2012-12-07 17:11:14

标签: vb.net url visual-studio-2012

我得到了"类型' System.ComponentModel.Win32Exception'的第一次机会异常。发生在System.dll"我尝试在VB中打开URL时出错。我已尝试多种方式打开网站,但所有人都返回了此错误。我现在使用的代码就是这个

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: 

2 个答案:

答案 0 :(得分: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属性会更有意义。

编辑:
要在系统的默认浏览器中打开网址,只需将programName设置为“start”即可。

Dim programName as String = "start"

这是Windows中的一个捷径,可以自动打开默认浏览器的URL。

答案 1 :(得分:0)

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

我的猜测(没有详细信息 - 将来尝试发布Exception堆栈跟踪),将无法确定您的默认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

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