Reputation: 20725
I'm reading the value of a registry key:
Microsoft.Win32.RegistryKey key;
key = *someLongPathHere*;
and displaying the value to a label:
string a = (string)key.GetValue("");//a default value
label1.Text = a;
It displays:
{F241C880-6982-4CE5-8CF7-7085BA96DA5A}
which is mostly correct, except for the first underscore, which exists in the original value:
{_F241C880-6982-4CE5-8CF7-7085BA96DA5A}
Why does it happen? i.e the missing underscore?
Also, after reading the key, do I have to close the key or anything? How can I do it?
Upvotes: 0
Views: 1206
Reputation: 613572
It's easy enough to test whether or not there is a systematic problem with string values containing underscores. You can simply create such a value in the registry editor and then read it into your C# program with GetValue()
. When you do so you'll discover that the C# registry code doesn't lose underscores. So, there must be some other explanation for your problem.
My best guess is that your label component does not display the underscore. I'm not very familiar with the C# UI frameworks but that seems plausible. Try looking at the value of a
under the debugger rather than in a label caption on your UI.
The other thing that comes to mind is that you have registry redirection because you have an x86 process running on x64, and your key is under a redirected key, HKLM\Software, for example. Perhaps if you look under the Wow6432Node you will see the underscore discrepancy.
As for managing the life of the key, the key is backed by an unmanged resource. Namely a Windows HKEY
. The RegistryKey
class implements IDisposable
and so you should wrap your keys in a using
statement.
Upvotes: 3