2009-10-13 186 views
3

我正在使用以下功能打開用戶的默認Web瀏覽器。打開默認Web瀏覽器

Public Function ShowHelp(ByVal url As String) As System.Diagnostics.Process 
    Dim startInfo As New Diagnostics.ProcessStartInfo() 
    startInfo.FileName = url 
    startInfo.WindowStyle = ProcessWindowStyle.Maximized 
    Return System.Diagnostics.Process.Start(startInfo) 
End Function 

有幾次該函數返回的錯誤(用戶機)「系統找不到指定的文件」

我想用戶沒有設置默認的Web瀏覽器。 爲什麼我得到這個錯誤?如何在調用此函數之前添加默認的Web瀏覽器檢查?

+0

你確定它的默認瀏覽器? – 2009-10-13 18:54:33

+0

請原諒我?我不安靜地理解你的評論 – OrElse 2009-10-13 19:05:40

回答

0

這是在C#中,但這是一個很好的文章:

http://ryanfarley.com/blog/archive/2004/05/16/649.aspx

這裏的C#爲VB.NET:

Private Function getDefaultBrowser() As String 
    Dim browser As String = String.Empty 
    Dim key As RegistryKey = Nothing 
    Try 
     key = Registry.ClassesRoot.OpenSubKey("HTTP\shell\open\command", False) 

     'trim off quotes 
     browser = key.GetValue(Nothing).ToString().ToLower().Replace("""", "") 
     If Not browser.EndsWith("exe") Then 
      'get rid of everything after the ".exe" 
      browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4) 
     End If 
    Finally 
     If key IsNot Nothing Then 
      key.Close() 
     End If 
    End Try 
    Return browser 
End Function 
1

這是推出正確的方式瀏覽器一般都帶有URL,但如果失敗,我只會捕獲該特定的異常,然後嘗試撥打iexplore <url>來打開IE中的URL,因爲它是博客und可以安裝在任何Windows系統上。 (我假設你沒有位置定位的單聲道/ Linux操作系統。)

+0

各種法律案件迫使微軟在沒有IE的情況下創建了許多Windows版本 - 你不能假定它存在。 – MarkJ 2009-10-13 18:56:32

+0

@MarkJ:是的,我現在記得那個。儘管如此,它往往存在。在我看來,這是公平的最後手段。 – Noldorin 2009-10-13 20:52:29

0

如果你在Windows上運行,下面的命令行應該在任何地方工作:

rundll32 url.dll,FileProtocolHandler <your_url> 

其中< YOUR_URL>是網頁要導航到的網址。

Public Function ShowHelp(ByVal url As String) As System.Diagnostics.Process 
     Dim startInfo As New Diagnostics.ProcessStartInfo() 
     startInfo.FileName = "rundll32 url.dll,FileProtocolHandler" 
     startInfo.Arguments = url 
     startInfo.WindowStyle = ProcessWindowStyle.Maximized 
     Return System.Diagnostics.Process.Start(startInfo) 
End Function 
0

如果你想dislay一個文件名「.html」或‘HTM’的兩端,然後你可以將它傳遞給的Process.Start()方法。同樣可以使用URL。

(必須旗組得到的Process.Start()使用shell的方法。)

2
Private Sub helpRichTextBox_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs) Handles helpRichTextBox.LinkClicked 
     System.Diagnostics.Process.Start(e.LinkText) 
    End Sub 
+0

唯一的一個正確答案 – abatishchev 2011-03-04 14:43:57

相關問題