【问题标题】:Issues about opening the default browser关于打开默认浏览器的问题
【发布时间】:2021-06-01 01:45:27
【问题描述】:

我想使用默认浏览器(在我的情况下为 Chrome)从 VB.NET 应用程序打开网页。

我使用这个代码:

Dim BrowserRegistryString As String = My.Computer.Registry.ClassesRoot.OpenSubKey("\http\shell\open\command\").GetValue("").ToString
Dim DefaultBrowserPath As String = System.Text.RegularExpressions.Regex.Match(BrowserRegistryString, "(\"".*?\"")").Captures(0).ToString
Process.Start(DefaultBrowserPath, "http://www.example.com")

它工作得很好,除了它在 Microsoft Edge 中打开网站,如果我在 shell 中使用“start http://www.example.com”,它会在 Chrome 中打开网站。 Chrome 被设置为我的默认浏览器(来自 Windows 10 配置面板),所以...

...那个谜是从哪里来的?

【问题讨论】:

  • 如果你想打开默认的WebBrowser,为什么不直接Process.Start("http://www.example.com")
  • 因为它不起作用。我得到了这个异常:“System.ComponentModel.Win32Exception:'找不到文件'
  • 您使用的是什么 .Net 版本? + 您是否尝试过运行提升的应用程序?或者添加runas 动词?
  • 我使用了 .NET 5.0 我不知道“运行提升的应用程序”和“添加 runas 动词”是什么意思。 ://
  • 在任何.Net Core 应用程序(或从.Net Core 派生,如.Net 5)中,UseShellExecutefalse,而在.Net Framework 中为true。因此,您需要使用Process.ProcessStartInfo 将其设置为true。 -- elevated 应用程序以管理员权限运行,与使用runas verb 相同,要求用户授予调用进程管理员权限。

标签: vb.net webbrowser-control


【解决方案1】:

这是我使用过的代码,它就像一个魅力。 我想有更好的方法来提取没有参数的默认浏览器路径...

Public Function GetDefaultBrowser() As String
        Dim strDefaultBrowserPathTemp As String
        Dim intPosDefaultBrowserFileExtension As Int16
        Using userChoiceKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice")
            If userChoiceKey IsNot Nothing Then
                Dim progIdValue As Object = userChoiceKey.GetValue("Progid")
                If progIdValue IsNot Nothing Then
                    strDefaultBrowserPathTemp = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\" & userChoiceKey.GetValue("Progid") & "\shell\open\command", "", Nothing).ToString
                    intPosDefaultBrowserFileExtension = strDefaultBrowserPathTemp.IndexOf(".exe" & Chr(34))
                    Return Left(strDefaultBrowserPathTemp, intPosDefaultBrowserFileExtension + Len(".exe" & Chr(34)))
                End If
            End If
        End Using
End Function

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 2011-07-01
    • 2011-11-19
    • 1970-01-01
    • 2010-09-14
    • 2011-05-09
    • 2014-12-06
    相关资源
    最近更新 更多