2012-12-07 128 views
2

我收到“System.dll中发生的类型为”System.ComponentModel.Win32Exception“的第一个机会异常错误。我尝试了多种打开网站的方式,但都返回了这个错误。我现在使用的代码是这样的当我尝试在VB中打开一个URL时,出现错误当我尝试在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: 
+0

我无法使用您提供的代码重现此操作。 'Process.Start'用shell执行它,所以它应该可以工作。 – vcsjones

+0

使用Chrome作为默认浏览器,在我的电脑上工作得很好。什么是您的默认浏览器? – theGD

回答

0

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

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,你可以简单地拥有你的程序名设置为“启动”。

Dim programName as String = "start" 

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

+0

谢谢,正如我所说,我真的很新,并且我不确定webbrowser控件是什么以及如何填充它,并且有没有办法选择默认浏览器,而不是IE? –

+0

@LewisLappin:Process.Start命令将运行该程序有权执行的任何可执行文件。关键是你必须知道正确的可执行文件的名称,除非你想使用默认的。如果您想使用默认浏览器,则更简单。我会发布一个编辑到我的答案。 –

+0

谢谢,但它仍然抛出我的错误。我更新了代码: Dim bitesize As String =“http://www.bbc.co.uk/bitesize/standard/” Dim programName As String =“start” Process.Start(programName &“”&bitesize) 但它给了我同样的错误,我不得不从头开始。 –

0

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

我的猜测(没有详细信息 - 未来,尝试发布异常堆栈跟踪),将是您的默认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 

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

如果你仍然有问题,张贴堆栈跟踪,可以帮助照亮什么错多一点光。

+0

谢谢,很高兴知道我做了正确的事情。我查了一下我的默认浏览器是什么,它说“未知的应用程序”,我改变了,但只有选择将其更改为IE出现(我使用铬作为我的默认,似乎工作正常),但我仍然收到错误。另外,我不太清楚异常堆栈跟踪的含义,但我想我可能已经找到它了,是否在第一次机会异常框中单击查看详细信息? –

+0

如何查找堆栈跟踪 - > http://stackoverflow.com/questions/945193/how-do-i-find-the-stack-trace-in-visual-studio –

+0

谢谢 这是它: > \t Study Time !.exe!WindowsApplication1.Revise.Button1_Click(Object sender,System.EventArgs e)Line 7 \t Basic –

相关问题