How to search for specific value in Registry keys

后端 未结 4 749
暗喜
暗喜 2020-12-15 12:19

How can I search for specific value in the registry keys?

For example I want to search for XXX in

HKEY_CLASSES_ROOT\\Installer\\Products
         


        
4条回答
  •  失恋的感觉
    2020-12-15 12:47

    This method will search a specified registry key for the first subkey that contains a specified value. If the key is found then the specified value is returned. Searchign is only one level deep. If you require deeper searching then I suggest modifying this code to make use of recursion. Searching is case-sensitive but again you can modify that if required.

    private string SearchKey(string keyname, string data, string valueToFind, string returnValue)
    {
        RegistryKey uninstallKey = Registry.LocalMachine.OpenSubKey(keyname);
        var programs = uninstallKey.GetSubKeyNames();
    
        foreach (var program in programs)
        {
            RegistryKey subkey = uninstallKey.OpenSubKey(program);
            if (string.Equals(valueToFind, subkey.GetValue(data, string.Empty).ToString(), StringComparison.CurrentCulture))
            {
                return subkey.GetValue(returnValue).ToString();
            }
        }
    
        return string.Empty;
    }
    

    Example usage

    // This code will find the version of Chrome (32 bit) installed
    string version = this.SearchKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall", "DisplayName", "Google Chrome", "DisplayVersion");
    

提交回复
热议问题