打开默认的网络浏览器

3
我正在使用以下函数来打开用户的默认网络浏览器。
 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

这个函数在用户的机器上出现了几次错误,错误信息为"系统找不到指定的文件"。

我猜测用户没有设置默认的网络浏览器。为什么会出现这个错误?在调用此函数之前,如何添加一个默认的网络浏览器检查?


你确定是默认浏览器吗? - Johnno Nolan
对不起,我不太理解您的评论。 - OrElse
5个回答

3
 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

1
这是一种通用的启动浏览器并打开URL的正确方法,但如果失败了,我会捕获那个特定的异常,然后尝试调用来在IE中打开该URL,因为它绑定到任何Windows系统上都已安装。 (我假设您不是针对Mono / Linux。)

各种法律案件迫使微软创建了许多没有IE的Windows版本 - 你不能假设它存在。 - MarkJ
@MarkJ:啊,是的,我现在记得了。不过,往往情况下它还是存在的。在我看来,这是一个公平的最后手段。 - Noldorin

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

0

如果你在Windows上运行,下面的命令行应该可以在任何地方工作:

rundll32 url.dll,FileProtocolHandler <your_url>

其中 <your_url> 是要导航到的网页 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
如果您想显示以“.html”或“htm”结尾的文件,则可以将其传递给Process.Start()方法。同样的方法也可能适用于URL。
(您必须设置标志,使Process.Start()使用shell方法。)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接