How to save a registry key value to string

Using Microsoft's example of scrolling registry subkeys we have the following code:

        using (RegistryKey tempKey = test9999.OpenSubKey(subKeyName))
      foreach (string valueName in tempKey.GetValueNames())
              Console.WriteLine("{0,-8}: {1}", valueName,

If the expression - tempKey.GetValue(valueName).ToString() - will write out to the console as a string why won't the following assignment work:

string SubValue = string.Empty;
SubValue  = tempKey.GetValue(valueName).ToString());

In debug mode, SubValue is assigned:  ""

If I evaluate the contents of 'valueName' by hovering over the variable during debug it shows the true value "My String Value"

Why would this be?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel JungesCommented:
try the follow:
SubValue  = Convert.ToString( tempKey.GetValue(valueName) );

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Looks like you have an extra parenthesis, but that is probably just a typo.

Is it throwing an error or anything, or is SubValue just staying blank?
John500Author Commented:
SubValue just stays blank - no error is thrown.  I tried the Convert.ToString(....) approach but that made no difference.

See the picture below,
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

So, are you sure that there is a key in your registry with a name of "MyStringValue"?
John500Author Commented:
Yes, absolutely
John500Author Commented:
The picture above shows the runtime value of 'valueName' which is 'MyStringValue' ... this is gotten from the registry.
John500Author Commented:
Instead of getting the value I really need to be getting the name.  That's why 'valueName' shows the name but there was no value.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.