Friday, August 16, 2013

Find installed browser version from Registry

More often than not, there would be need to find version of browser installed on a machine on which Automation is run.

After lot of googling and validations of suggestions on different blogs, I could figure out registry paths to get browser version for Internet Explorer, Firefox and Chrome on 32-bit and 64-bit machines.

I thought it would be good idea to blog this so that others can save time rather than browsing through multiple sites and do trail and error.

Internet Explorer:
For 32-bit and 64-bit Machines, registry path:
HKLM\SOFTWARE\Microsoft\Internet Explorer\Version

For Windows 8:
HKLM\SOFTWARE\Microsoft\Internet Explorer\svcVersion

Method to retrieve version programmatically:
private static void GetIEVersion()
    {
        RegistryKey regKey = Registry.LocalMachine;
        Console.WriteLine("ie-" + regKey.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer").GetValue("Version")); //For Windows 8 the key name is 'svcVersion'
    }

Firefox:
For 32-bit machine:
HKLM\SOFTWARE\Mozilla\Mozilla Firefox\CurrentVersion
For 64-bit machine:
HKLM\SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox\CurrentVersion


Method to retrieve version programmatically:
private static void GetFirefoxVersion()
    {
        string wowNode = string.Empty;
        if (Environment.Is64BitOperatingSystem) wowNode = @"Wow6432Node\";
        RegistryKey regKey = Registry.LocalMachine;
        Console.WriteLine("firefox-" +
          regKey.OpenSubKey(@"Software\" + wowNode + @"Mozilla\Mozilla Firefox")
                .GetValue("CurrentVersion"));
    }
Chrome:
For 32-bit machines:
HKLM\SOFTWARE\Google\Update\Clients

For 64-bit machines:
HKLM\SOFTWARE\Wow6432Node\Google\Update\Clients

Not done yet, chrome is little complex. 
The above path lists multiple GUIDs {CLSID}. Search for GUID subkey which has key "name" value as "Google Chrome"
Under same subkey chrome version will be present in key named "pv"

If chrome is installed only for current user, change the root of registry to HKCU
HKCU\Software\Google\Update\Clients

On 64-bit machine if there is no key as mentioned above, try searching in same path as that of 32-bit machine.

Method to retrieve version programmatically:
private static void GetChromeVersion()
    {
        string wowNode = string.Empty;
        if (Environment.Is64BitOperatingSystem) wowNode = @"Wow6432Node\";

        RegistryKey regKey = Registry.LocalMachine;
        RegistryKey keyPath = regKey.OpenSubKey(@"Software\" + wowNode + @"Google\Update\Clients");

        if (keyPath == null)
        {
            regKey = Registry.CurrentUser;
            keyPath = regKey.OpenSubKey(@"Software\" + wowNode + @"Google\Update\Clients");
        }      

        if (keyPath == null)
        {
            regKey = Registry.LocalMachine;
            keyPath = regKey.OpenSubKey(@"Software\Google\Update\Clients");
        }

        if (keyPath == null)
        {
            regKey = Registry.CurrentUser;
            keyPath = regKey.OpenSubKey(@"Software\Google\Update\Clients");
        }

        if (keyPath != null)
        {
            string[] subKeys = keyPath.GetSubKeyNames();
            foreach (string subKey in subKeys)
            {
                object value = keyPath.OpenSubKey(subKey).GetValue("name");
                bool found = false;
                if (value != null)
                    found =
                        value.ToString()
                             .Equals("Google Chrome", StringComparison.InvariantCultureIgnoreCase);
                if (found)
                {
                    Console.WriteLine("chrome-" + keyPath.OpenSubKey(subKey).GetValue("pv"));
                    break;
                }
            }
        }
        else
        {
            Console.WriteLine("registry key not found for chrome");
        }
    }