C# Windows RegKey - 默认浏览器应用程序路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/813058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows RegKey - Default Browser Application Path
提问by BuddyJoe
What RegKey can you get the default browser application's path from?
您可以从哪个 RegKey 获取默认浏览器应用程序的路径?
Best way to get to it from C#/.NET?
从 C#/.NET 获取它的最佳方式?
采纳答案by Jon B
Here's the key you want:
这是你想要的钥匙:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\http\shell\open\command
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\http\shell\open\command
And here's a quick registry tutorial for C#, if you need it.
Edit:
编辑:
For per-user settings, use this key:
对于每用户设置,请使用此键:
HKEY_CLASSES_ROOT\http\shell\open\command
HKEY_CLASSES_ROOT\http\shell\open\command
(HKCR has both machine and user settings, user takes priority).
(HKCR 有机器设置和用户设置,用户优先)。
Note that this might not work on Vista. For more info, see here.
请注意,这可能不适用于 Vista。有关更多信息,请参见此处。
回答by DevT
for windows 7 default browser path save in following registry key
对于 Windows 7 默认浏览器路径保存在以下注册表项中
HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\ Associations\UrlAssociations\http
by using c# you can get it as follows -
通过使用 c# 你可以得到它如下 -
RegistryKey regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\shell\Associations\UrlAssociations\http\UserChoice", false);
string browser = regkey.GetValue("Progid").ToString();
回答by Vincent Rich
Based on your answers I wrote this sample code that should do what you want (not tested)
根据您的回答,我编写了此示例代码,它应该可以满足您的要求(未测试)
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;
}
回答by winsetter
I just made a function for this:
我刚刚为此做了一个函数:
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));
}