7

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

HudsonPH
  • 1,838
  • 2
  • 22
  • 38
  • `root.OpenSubKey(keyname)` is returning null. That is why you get that error. – Hogan May 22 '11 at 16:15
  • Is that not the proper code then? I'm assuming ... Is there another way to open up the key? –  May 22 '11 at 16:16
  • 2
    From the docs " If the specified subkey cannot be found, then null is returned." – Hogan May 22 '11 at 16:27
  • Yes, I agree - the path is incorrect... (somehow) but I am confused about this part as the key does exist. Let me triple check! –  May 22 '11 at 16:31
  • I went into registry and did "copy key name" and it returned the same key that I am using in my program. I also made sure that the "keyname" exists - and it DOES have a value. So the path seems to be correct here. –  May 22 '11 at 16:35

3 Answers3

8

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());
    }
 }
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • the GetValueNames() is working fine - it's getting all of the values for the given subkey. But I then want to find the string value of the values found above. This is where I'm running into problems. I am 99.99 percent sure that the name is written exactly as it's found in registry. It seems I'll have to do some more investigating, though. –  May 22 '11 at 16:38
  • The value is not the same as the key. See code sample above. – Hogan May 22 '11 at 16:40
  • hit the nail on the head. + 1 / Correct answer. –  May 22 '11 at 16:49
3

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I know this makes me sound like an ***hole but feel free to just decline... Would you be willing to make a registry key under "HKCU\software" and then add another key under this key, and then ... give this final key a value? Once the keys are there you might be able to see where I am messing up? I don't think that my OS is the issue as I'm doing this all on one machine right now (32 bit). –  May 22 '11 at 16:41
  • 1
    @Evan: Darn, I thought the 64-bit suggestion would be the answer. I suppose I can do that... Give me a minute. – Cody Gray - on strike May 22 '11 at 16:42
  • @Evan: The problem is that `root` doesn't have any subkeys! It has one *value*, but no subkeys. – Cody Gray - on strike May 22 '11 at 16:51
  • So would you suggest I use the provided by Hogan? It gets the value just fine, but it does not return the key name. Something I can surely modify to work but - just asking. –  May 22 '11 at 16:53
  • @Evan: I have no idea. It's hard to tell what you're trying to do. I think you're confusing the terminology value and key. If the code does what you want, then use it. – Cody Gray - on strike May 22 '11 at 16:54
1

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() );
        }
    }
}
gabriel290687
  • 113
  • 1
  • 8