I have a legacy (I didn’t build it) application running on an x64 environment (Win7). I have a sneaking suspicion it was run on a 32-bit environment before.
Anyway, I see calls to Registry.GetValue(key, value, default).
It seems like the default value is ignored.
Check out this crazy code:
// Up above the sky, so high
using Microsoft.Win32;
// ...
string location = "HKEY_LOCAL_MACHINE\SOFTWARE\..."; // ...
// ...
string registryValue = (string)Registry.GetValue(location, "Uri", "http://localhost/");
if (string.isNullOrEmpty(registryValue) {
throw new Exception("What the ... ?!");
}
In a comparable example, the exception is seriously being thrown. (Actually, a null-reference exception appears despite the default value).
And I checked that I have the registry keys all the way up to the last level; they’re all in my registry.
This works for someone, but not for me.
Is this a bug? What’s going on here?
Most likely you are being caught out by registry redirection. You have a 32 bit process running on a 64 bit system. So
HKLM\Softwareis redirected toHKLM\Software\Wow6432Node.When the key does not exist, the
Registry.GetValuereturnsnullrather than the default value and so the exception is thrown.