2009-10-13 238 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

相关问题