【问题标题】:Windows RegKey - Default Browser Application PathWindows RegKey - 默认浏览器应用程序路径
【发布时间】:2009-05-01 20:13:03
【问题描述】:

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

从 C#/.NET 获取它的最佳方式?

【问题讨论】:

  • 您不应探查注册表以尝试启动默认 Web 浏览器。你想做什么?
  • 我不想启动默认浏览器。我有一个程序可以根据某人的浏览器偏好做出一些不同的选择。

标签: c# .net windows registry


【解决方案1】:

这是你想要的钥匙:

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\http\shell\open\command

如果您需要,这里有一个快速的registry tutorial for C#

编辑

对于每个用户的设置,使用这个键:

HKEY_CLASSES_ROOT\http\shell\open\command

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

请注意,这可能不适用于 Vista。欲了解更多信息,see here

【讨论】:

  • 您几乎肯定想要“HKEY_CLASSES_ROOT”,而不是 HKEY_LOCAL_MACHINE。 HKEY_CLASSES_ROOT 将始终返回用户期望的浏览器。
【解决方案2】:

对于 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();

【讨论】:

  • WINdow 10 也一样!
【解决方案3】:

根据您的回答,我编写了这个示例代码,应该可以满足您的需求(未经测试)

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

【讨论】:

  • 在 Win7 上,“Progid”似乎不包含链接。它包含要在“HKCR/FetchedProgramId”下的注册表中查找的程序 ID(FetchedProgramId 是之前获取的程序 id 值)。在该键下,又是一个“\shell\open\command”,您可以在其中找到实际路径。
  • 这似乎没有给出 Windows 10 上的路径。它只返回 IE.HTTP 的值
【解决方案4】:

我刚刚为此做了一个函数:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多