• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

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,
              tempKey.GetValue(valueName).ToString());
        }

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?

Thanks
   
0
John500
Asked:
John500
  • 4
  • 2
2 Solutions
 
Daniel JungesCommented:
try the follow:
...
SubValue  = Convert.ToString( tempKey.GetValue(valueName) );
0
 
ChetOS82Commented:
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?
0
 
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,
Thanks
reg-routine.jpg
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
ChetOS82Commented:
So, are you sure that there is a key in your registry with a name of "MyStringValue"?
0
 
John500Author Commented:
Yes, absolutely
0
 
John500Author Commented:
The picture above shows the runtime value of 'valueName' which is 'MyStringValue' ... this is gotten from the registry.
0
 
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.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now