sfeng
sfeng

Reputation: 44

How can I get the Really Value of the Windows Registry Keys by C# with .Net Framework?

I'm trying to read a value of Windows Registry Key using C# with .Net Framework, the real value of the key is %SystemRoot%, but the value I've got is C:\Windows. How Can I get the string %SystemRoot% instead of the string C:\Windows?

Here's the code I used to read the value of the key:

RegistryKey rkCurrentUser = Registry.CurrentUser.OpenSubKey("Environment");

foreach (string name in rkCurrentUser.GetValueNames())
{
    Console.WriteLine("The type of the {0} is {1}:", rkCurrentUser.GetValue(name), rkCurrentUser.GetValueKind(name));
}

Upvotes: 1

Views: 234

Answers (1)

Fabio M.
Fabio M.

Reputation: 296

If I understood well your question, you need just to change your code to this:

Console.WriteLine("The type of the {0} is {1}:", rkCurrentUser.GetValue(name,"",RegistryValueOptions.DoNotExpandEnvironmentNames), rkCurrentUser.GetValueKind(name));

Check official documentation here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registryvalueoptions?view=netframework-4.7.2

Upvotes: 2

Related Questions