getting a list of installed browsers on the comput

2020-06-23 09:29发布

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.

标签: c#
4条回答
家丑人穷心不美
2楼-- · 2020-06-23 10:00

Look at localmachine registry...

Microsoft.Win32.RegistryKey key =           
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");
var browsers = key.GetSubKeyNames();
查看更多
我想做一个坏孩纸
3楼-- · 2020-06-23 10:01

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
}
查看更多
戒情不戒烟
4楼-- · 2020-06-23 10:02

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/

查看更多
爷的心禁止访问
5楼-- · 2020-06-23 10:09

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; }
}
查看更多
登录 后发表回答