'getting a list of installed browsers on the computer
I would like to know is there are any possibility to get a list of installed browsers on the computer using c#? I'm using Selenium WebDriver in my task and I need to know which browsers are installed because in Selenium I can only run a specific browser, for example for Firefox it will be:
IWebDriver driver = new FirefoxDriver();
I will appreciate any help.
Solution 1:[1]
You also need to take into account the machine architecture (x64 vs x86) and the fact that Microsoft Edge will not be under the specified key. Here is what I ended up using (based on multiple solutions found online):
private List<Browser> GetBrowsers()
{
RegistryKey browserKeys;
browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet");
if (browserKeys == null)
browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");
string[] browserNames = browserKeys.GetSubKeyNames();
List<Browser> 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 = browserKeyPath.GetValue(null).ToString().StripQuotes();
browsers.Add(browser);
if (browser.Path != null)
browser.Version = FileVersionInfo.GetVersionInfo(browser.Path).FileVersion;
else
browser.Version = "unknown";
}
Browser edgeBrowser = GetEdgeVersion();
if (edgeBrowser != null)
{
browsers.Add(edgeBrowser);
}
return browsers;
}
private Browser GetEdgeVersion()
{
RegistryKey edgeKey =
Registry.CurrentUser.OpenSubKey(
@"SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\Schemas");
if (edgeKey != null)
{
string version = edgeKey.GetValue("PackageFullName").ToString().StripQuotes();
Match result = Regex.Match(version, "(((([0-9.])\\d)+){1})");
if (result.Success)
{
return new Browser
{
Name = "MicrosoftEdge",
Version = result.Value
};
}
}
return null;
}
And the object returned is a simple DTO:
public class Browser{
public string Name { get; set; }
public string Path { get; set; }
public string Version { get; set; }
}
Solution 2:[2]
I've written a NuGet package for this: https://www.nuget.org/packages/MintPlayer.PlatformBrowser/ targetting .net core.
You can get a list of all installed webbrowsers (including Edge) and the default webbrowser. I've also written a package with a dialog to let you pick a browser: https://www.nuget.org/packages/MintPlayer.BrowserDialog/
Solution 3:[3]
As far as I know there is no list of browsers in Windows.
However you could check for browser's existence by simply testing the *.exe file's existence:
if (File.Exists(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe") ||
File.Exists(@"C:\Program Files\Google\Chrome\Application\chrome.exe")) {
// chrome is installed
}
if (File.Exists(@"C:\Program Files (x86)\Mozilla Firefox\firefox.exe") ||
File.Exists(@"C:\Program Files\Mozilla Firefox\firefox.exe") {
// firefox is installed
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Radu Cojocari |
| Solution 2 | Pieterjan |
| Solution 3 | Dávid Molnár |
