2012-12-07 132 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 –

相關問題