Adrian17
Adrian17

Reputation: 443

Registry.GetValue automatically expands environment variables - any way to avoid that?

When using Registry.GetValue(key, name, null), instead of:

C:\Windows\system32

I would like to retrieve the raw registry entry:

%SystemRoot%\system32

This is possible in C with WinAPI, but I don't see a way to do that with .NET.

Upvotes: 2

Views: 837

Answers (2)

Donal
Donal

Reputation: 32713

You need to pass it RegistryValueOptions.DoNotExpandEnvironmentNames. For example:

var key = @"SOFTWARE\Classes\.library-ms\ShellNew";
var name = "IconPath";

var regval = Registry.LocalMachine.OpenSubKey(key).GetValue(name, "",
RegistryValueOptions.DoNotExpandEnvironmentNames);

Console.WriteLine(regval);

output is: %SystemRoot%\System32\imageres.dll,-1001

Upvotes: 6

Rufus L
Rufus L

Reputation: 37060

From what I can tell, it does what you want by default:

Registry.SetValue(@"HKEY_CURRENT_USER\dummy", "pathTest", @"%SystemRoot%\system32");
Console.WriteLine(Registry.GetValue(@"HKEY_CURRENT_USER\dummy", "pathTest", null));

// Output:
// %SystemRoot%\system32

Upvotes: 0

Related Questions