且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在我的Windows应用程序中,我想检查运行该应用程序的机器上是否安装了firefox。我可以吗?

更新时间:2023-01-12 12:07:41

请看这里:获取已安装的浏览器和版本C#| Robb Sadler的博客 [ ^ ] - 它从teh注册表中收集已安装的浏览器。
See here: Getting Installed Browsers and Version C# | Robb Sadler's Weblog[^] - it collects installed browsers from teh registry.


要在您的应用程序中导入它,请使用此





To import it in your application use this


foreach (Browser browser in GetBrowsers())
{
Console.WriteLine(string.Format("{0}: \n\tPath: {1} \n\tVersion: {2} \n\tIcon: {3}", browser.Name, browser.Path, browser.Version, browser.IconPath));
}
Console.ReadKey();
}




public static List<Browser> GetBrowsers()
{
RegistryKey browserKeys;
//on 64bit the browsers are in a different location
browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet");
if (browserKeys == null)
browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");
string[] browserNames = browserKeys.GetSubKeyNames();
var browsers = new List<Browser>();
for (int i = 0; i < browserNames.Length; i++)
{
Browser browser = new Browser();
RegistryKey browserKey = browserKeys.OpenSubKey(browserNames[i]);
browser.Name = (string)browserKey.GetValue(null);
RegistryKey browserKeyPath = browserKey.OpenSubKey(@"shell\open\command");
browser.Path = (string)browserKeyPath.GetValue(null).ToString().StripQuotes();
RegistryKey browserIconPath = browserKey.OpenSubKey(@"DefaultIcon");
browser.IconPath = (string)browserIconPath.GetValue(null).ToString().StripQuotes();
browsers.Add(browser);
if (browser.Path != null)
browser.Version = FileVersionInfo.GetVersionInfo(browser.Path).FileVersion;
else
browser.Version = "unknown";
}
return browsers;
}
}


internal static class Extensions
{
///
/// if string begins and ends with quotes, they are removed
///
internal static String StripQuotes(this String s)
{
if (s.EndsWith("\"") && s.StartsWith("\""))
{
return s.Substring(1, s.Length - 2);
}
else
{
return s;
}
}
}


class Browser
{
public string Name { get; set; }
public string Path { get; set; }
public string IconPath { get; set; }
public string Version { get; set; }
}
}