How do I open default browser to home page in C#?

别等时光非礼了梦想. 提交于 2019-12-25 09:04:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!