如何在C#中将默认浏览器打开到主页?
问题描述:
下面的问题有答案:“如何打开默认浏览器到C#中的URL?”但是,如何在不导航到URL的情况下打开C#中的默认浏览器?期望的行为是默认浏览器打开主页,或使用启动行为(例如,在Firefox选项“Firefox启动时”中定义)。如何在C#中将默认浏览器打开到主页?
答
这应做到:
Process p = new Process();
p.StartInfo = new StartInfo(url) {UseShellExecute = true};
p.Start();
编辑:这将有效的URL工作。正如上面的评论所说,这不适用于http://about:home。
编辑#2:我会保留以前的代码,以防万一它有助于任何人。自从上面的评论我一直在寻找如何做到这一点,并在行动并不那么微不足道,这就是我为了启动默认浏览器,而不导航到任何URL
。
using (var assoc = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"))
{
using (var cr = Registry.ClassesRoot.OpenSubKey(assoc.GetValue("ProgId") + @"\shell\open\command"))
{
string loc = cr.GetValue("").ToString().Split('"')[1];
// In windows 10 if Microsoft edge is the default browser
// loc=C:\Windows\system32\LaunchWinApp.exe, so launch Microsoft Edge manually
// 'cause didn't figured it out how to launc ME with that exe
if (Path.GetFileNameWithoutExtension(loc) == "LaunchWinApp")
Process.Start("explorer", @"shell:Appsfolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge");
else
Process.Start(loc);
}
}
我在我的机器(Win10)测试,并制定了每一个默认浏览器的开关。希望现在有帮助。
你正在尝试使用哪种操作系统? – itsme86
这并不容易,因为你想要的URL是'about:home',但是你不能把它交给操作系统。这意味着你必须弄清楚默认浏览器是什么,获取它的路径,然后将URL作为命令行参数传递。 – Will
@ itsme86,平台是WPF,Windows 7/8/10。 –