Get value of registry key C#

2019-06-21 19:35发布

问题:

I have already looked at existing topics, so please try to refrain from dropping links here.

I want to get the value of a registry key - plain and simple. Here is what I have so far.

Registry: 1) Made a key under

Current_User\Software\Custom_Subkey\Custom_Value\Custom_key\string_value

I am trying to find the string_value

        string reg_subKey = "Software\\Custom_Subkey\\Custom_Value";

        RegistryKey root = Registry.CurrentUser.CreateSubKey(reg_subKey);


        foreach (string keyname in root.GetValueNames())
        {
            textBox4.AppendText(keyname.ToString() + Environment.NewLine);

// Appends the following data to textBox4 once the foreach is completed:
// Header1
// Header2
// Header3
// Header4
// Header5

// Now I want to get the VALUES of each header:

            using (RegistryKey key = root.OpenSubKey(keyname))
            {

**// THIS LINE GETS HIGHLIGHTED WITH THE FOLLOWING ERROR:
"Object reference not set to an instance of an object.**"
                MessageBox.Show(key.ValueCount.ToString());
            }
        }

Hopefully this is a simple fix. I look forward to hearing your responses. Thanks, Evan

回答1:

I believe you want root.GetSubKeyNames() in the loop not GetValueNames()

While values is working to get the values I would suggest the following loop:

foreach(string keyname in root.GetSubKeyNames())
{
    // use key to get value and set textbox4


    using (RegistryKey key = root.OpenSubKey(keyname))
    {
       MessageBox.Show(key.ValueCount.ToString());
    }
 }


回答2:

The OpenSubKey method does not throw an exception if the specified subkey is not found. Instead, it simply returns null. It's your responsibility as a programmer to ensure that the appropriate key was found and opened by checking the return value of the method call.

Thus, my suspicion is that the registry key that you've specified is invalid. Open up Registry Editor (regedt32.exe), and verify that you can find the key in the registry exactly as written.

If you find that the registry key is indeed located exactly where you thought it was, then the problem may be related to the WOW64 subsystem, which allows 64-bit versions of Windows to run 64-bit apps. If the value was written to the registry by a 32-bit program, you won't be able to read it with the above code from a 64-bit program (or vice versa). The simplest way to check this is to change the compilation settings for your project. For example, if you're currently compiling for x86, then change to compiling for x64, or vice versa. Registry redirection may also be getting in your way; this will check for that as well.



回答3:

I wanted the very same thing and your code helped me, but as you said, it didn't work properly. So, I made some modifications and I think it works fine now! Try this:

//Just make the reference until "custom_subkey", not to the next one ("custom value")
string reg_subKey = "Software\\Custom_Subkey";

RegistryKey root = Registry.CurrentUser.CreateSubKey(reg_subKey);

//Use GetSubKeyNames, instead of GetValueNames, because now you are in a higher level
foreach (string keyname in root.GetSubKeyNames())
{
    using (RegistryKey key = root.OpenSubKey(keyname))
    {
        foreach (string valueName in key.GetValueNames())
        {
            MessageBox.Show(valueName);
            MessageBox.Show(key.GetValue(valueName).ToString() );
        }
    }
}