您可以从RegKey获取默认浏览器应用程序的路径吗?

从C#/ .NET获取它的最佳方法是什么?

有帮助吗?

解决方案

这是你想要的钥匙:

  

HKEY_LOCAL_MACHINE \ SOFTWARE \类\ HTTP \壳\开放\命令

如果需要,可以快速 C#注册表教程

修改

对于每用户设置,请使用以下密钥:

  

HKEY_CLASSES_ROOT \ HTTP \壳\开放\命令

(HKCR有机器和用户设置,用户优先)。

请注意,这可能不适用于Vista 。有关详细信息,请看这里

其他提示

for Windows 7默认浏览器路径保存在以下注册表项

 HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\ Associations\UrlAssociations\http

使用c#你可以得到如下 -

RegistryKey regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", false);

string browser = regkey.GetValue("Progid").ToString();

根据你的答案,我写了这个示例代码,应该做你想要的(未经测试)

public static string GetDefaultBrowserPath()
    {
        string defaultBrowserPath = null;
        RegistryKey regkey;

        // Check if we are on Vista or Higher
        OperatingSystem OS = Environment.OSVersion;
        if ((OS.Platform == PlatformID.Win32NT) && (OS.Version.Major >= 6))
        {
            regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\shell\\Associations\\UrlAssociations\\http\\UserChoice", false);
            if (regkey != null)
            {
                defaultBrowserPath = regkey.GetValue("Progid").ToString();
            }
            else
            {
                regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes\\IE.HTTP\\shell\\open\\command", false);
                defaultBrowserPath = regkey.GetValue("").ToString();
            }
        }
        else
        {
            regkey = Registry.ClassesRoot.OpenSubKey("http\\shell\\open\\command", false);
            defaultBrowserPath = regkey.GetValue("").ToString();
        }

        return defaultBrowserPath;
    }

我刚刚为此做了一个功能:

    public void launchBrowser(string url)
    {
        string browserName = "iexplore.exe";
        using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"))
        {
            if (userChoiceKey != null)
            {
                object progIdValue = userChoiceKey.GetValue("Progid");
                if (progIdValue != null)
                {
                    if(progIdValue.ToString().ToLower().Contains("chrome"))
                        browserName = "chrome.exe";
                    else if(progIdValue.ToString().ToLower().Contains("firefox"))
                        browserName = "firefox.exe";
                    else if (progIdValue.ToString().ToLower().Contains("safari"))
                        browserName = "safari.exe";
                    else if (progIdValue.ToString().ToLower().Contains("opera"))
                        browserName = "opera.exe";
                }
            }
        }

        Process.Start(new ProcessStartInfo(browserName, url));
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top