Microsoft.Win32.Registry的GetValue不使用默认值

ash*_*999 2 c# registry 64-bit

我有一个在x64环境(Win7)上运行的遗产(我没有构建它).我怀疑它之前在32位环境中运行.

无论如何,我看到了电话Registry.GetValue(key, value, default).

似乎忽略了默认值.

看看这个疯狂的代码:

// 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 ... ?!");
}
Run Code Online (Sandbox Code Playgroud)

在一个类似的例子中,严重抛出异常.(实际上,尽管存在默认值,仍会出现空引用异常).

我检查了我的注册表项一直到最后一级; 他们都在我的注册表中.

这适用于某人,但不适合我.

这是一个错误吗?这里发生了什么?

Dav*_*nan 9

很可能你被注册表重定向所困扰.您在64位系统上运行32位进程.因此HKLM\Software被重定向到HKLM\Software\Wow6432Node.

当密钥不存在时,Registry.GetValue返回null而不是默认值,因此抛出异常.

如果在指定的键中找不到该名称,则返回您提供的默认值,如果指定的键不存在,则返回null.