1

我试图弄清楚如何使菜单条项目将活动的 Windows 帐户默认浏览器打开到他们的主页。我已经尝试过Process.Start("about:blank")了,出于某种原因,这总是会打开 Internet Explorer 的about:blank页面。(我将 Google Chrome 作为我的默认浏览器,并将http://www.duckduckgo.com作为其在 Windows 7 Pro 上的主页。)

我知道我可以指定任何 URL 来打开默认浏览器,但是如何让他们选择的主页打开?我发现一些基于 C# 的文章需要查看注册表项,以便在每个浏览器中找到他们选择的主页。VB.Net 2017 中的过程是否相同/相似,我将如何去做?这是使用 VB.Net 2017 社区版,该项目是一个 Windows.Forms 桌面应用程序。

4

3 回答 3

0

我发现的唯一方法是手动查询注册表有关处理 http 协议的默认命令。

此代码的第一行将返回类似 的内容"C:\Program Files\Your Browser\browser.exe" -osint -url "%1",因此您想用%1着陆页替换。

然后,如果要Process.Start与命令行参数一起使用,第一个参数将是命令,第二个参数是参数。因此,我们需要在命令和参数列表之间拆分注册表字符串。正则表达式将完成这项工作

为了清楚起见,我省略了空检查和正则表达式成功。

Dim cmd = CStr(Registry.ClassesRoot.OpenSubKey("http\shell\open\command").GetValue(String.Empty))
cmd = cmd.Replace("%1","about:blank")
Dim r = new Regex("^""([^""]+)"" (.*)")
Dim m = r.Match(cmd)
Process.Start(m.Groups(1).Value, m.Groups(2).Value)
于 2018-05-09T12:45:31.453 回答
0

在这里找到了一些线索。

Dim readValue As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\
Associations\UrlAssociations\http\UserChoice", "Progid", Nothing).ToString

将为当前用户的浏览器提供一个标识符。

Dim path As String = My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\"
& readValue & "\shell\open\command", "", Nothing).ToString

将返回带有路径的运行命令。

添加一些代码以提取 EXE 并在不带参数的情况下运行它,例如;

 Dim DivArr As Char() = {Chr(34), "\"c}
'split into segments using quotes and back-slash seperators
 Dim parts() As String = path.Split(DivArr)
 'find first segment with period/full-stop
 Dim Executable As String = Array.Find(parts, Function(x) (x.Contains(".")))

Process.start(Executable) 
于 2018-05-09T17:08:06.613 回答
-1

你可以试试这个:

Process.Start("your_url_here eg. www.homepage.com etc.")

并且,如果它是您的默认浏览器,它将使用 google chrome 打开。

于 2018-05-09T12:49:33.673 回答