问题
The question below has the answer to "How do I open the default Browser to a URL in C#?" But how do I open the default browser in C# without navigating to a URL? Desired behavior is for the default browser to open to the home page, or use the startup behavior (for example, defined in the Firefox option "When Firefox starts").
How to open in default browser in C#
回答1:
This should do it:
Process p = new Process();
p.StartInfo = new StartInfo(url) {UseShellExecute = true};
p.Start();
EDIT: This will work for a valid URL. As the comment above say this will not work for http://about:home.
EDIT #2: I will keep the previous code in case it's helpful for anybody. Since the comment above I've been looking how to do it, and in deed was not so trivial, this is what I did in order to launch the default browser without navigating to any 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);
}
}
I tested it in my machine (Win10) and worked out for every default browser switch. Hope it helps now.
来源:https://stackoverflow.com/questions/43376033/how-do-i-open-default-browser-to-home-page-in-c