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"

これは、デフォルトのブラウザで自動的に URL を開く Windows のショートカットです。

于 2012-12-07T17:15:58.750 に答える
0

そのコード (ご存知のように) は、コンピューターの既定の Web ブラウザーでその Web サイトを開きます。あなたのコードは正しいです (ただし、メソッド名はおそらくリッスンしているコントロールと一致するはずなので、多くのイベント ハンドラーを含むフォームがある場合は少し読みやすくなります)。

私の推測では (詳細は不明です -- 将来、例外スタック トレースを投稿してみてください)、既定の Web ブラウザーを特定できない可能性があります。これを試してください: コントロール パネル\プログラム\既定のプログラム\関連付けの設定 .. を開き、HTTP プロトコルを見つけて (すべてのファイル拡張子の関連付けの後、下部に向かって)、関連付けられているアプリケーションをリセットします。

次に、これはもう 1 つの学習の良い機会です。問題が発生する可能性があるコード (別のプロセスの起動など) がある場合は、そのコードを 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 に答える