【发布时间】:2009-05-01 20:13:03
【问题描述】:
您可以从哪个 RegKey 获取默认浏览器应用程序的路径?
从 C#/.NET 获取它的最佳方式?
【问题讨论】:
-
您不应探查注册表以尝试启动默认 Web 浏览器。你想做什么?
-
我不想启动默认浏览器。我有一个程序可以根据某人的浏览器偏好做出一些不同的选择。
您可以从哪个 RegKey 获取默认浏览器应用程序的路径?
从 C#/.NET 获取它的最佳方式?
【问题讨论】:
这是你想要的钥匙:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\http\shell\open\command
如果您需要,这里有一个快速的registry tutorial for C#。
编辑:
对于每个用户的设置,使用这个键:
HKEY_CLASSES_ROOT\http\shell\open\command
(HKCR有机器和用户设置,用户优先)。
请注意,这可能不适用于 Vista。欲了解更多信息,see here。
【讨论】:
对于 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));
}
【讨论】: