如何在C#中打开默认浏览器到主页? [英] How do I open default browser to home page in C#?

查看:86
本文介绍了如何在C#中打开默认浏览器到主页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题的答案为如何在C#中打开默认浏览器的URL?但是,如何在不导航到URL的情况下用C#打开默认浏览器?期望的行为是默认浏览器打开到主页或使用启动行为(例如,在Firefox选项当Firefox启动时中定义)。

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").

如何在C#默认浏览器中打开

推荐答案

这应该做到:

Process p = new Process();
p.StartInfo = new StartInfo(url) {UseShellExecute = true};
p.Start();

编辑:这将对有效的URL有效。正如上面的评论所说,这不适用于 http:// about:home

EDIT: This will work for a valid URL. As the comment above say this will not work for http://about:home.

编辑#2 :我会保留前面的代码,以防对任何人有帮助。由于上面的评论一直在寻找中,而实际上并不是那么琐碎,这就是我为启动默认浏览器而不导航到任何 URL

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);
    }
}

我在计算机(Win10)中对其进行了测试并正常工作每个默认浏览器开关都退出。希望它能对您有所帮助。

I tested it in my machine (Win10) and worked out for every default browser switch. Hope it helps now.

这篇关于如何在C#中打开默认浏览器到主页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆