Search code examples
c#registry

Getting Data from Values in the Registry [C#]


My program contains quite a few values in the registry and getting the names of these values is not problematic at all; the real problem is getting the data from these particular values.

Here is a segment of my code. Assume "paths.mainKey" is "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node". Also assume that "paths.subKey" is "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Razer Chroma SDK" (obviously not my key, just using it as an example).

private void ReadRegistry()
{
    string[] allSubKeys = Registry.LocalMachine.OpenSubKey(paths.mainKey).GetSubKeyNames();
    if (allSubKeys.Contains("Razer Chroma SDK"))
    {
        string[] allDayPolicies = Registry.LocalMachine.OpenSubKey(paths.subKey).GetValueNames();
        foreach (string value in allDayPolicies)
        {
            //Read data from the values?
        }
    }
}

This is a visual representation of what I'm trying to get from the Registry.

Anyone know how to obtain this data?


Solution

  • You can use GetValue():

    private void ReadRegistry()
    {
        string[] allSubKeys = Registry.LocalMachine.OpenSubKey(paths.mainKey).GetSubKeyNames();
        if (allSubKeys.Contains("Razer Chroma SDK"))
        {
            var subKey = Registry.LocalMachine.OpenSubKey(paths.subKey);
            string[] allDayPolicies = subKey.GetValueNames();
            foreach (string name in allDayPolicies)
            {
                var value = subKey.GetValue(name);
                // do something with value
            }
        }
    }