[英]Microsoft.Win32.Registry's GetValue Doesn't Use Default Value
I have a legacy (I didn't build it) application running on an x64 environment (Win7). 我有一个在x64环境(Win7)上运行的遗产(我没有构建它)。 I have a sneaking suspicion it was run on a 32-bit environment before.
我怀疑它之前在32位环境中运行。
Anyway, I see calls to Registry.GetValue(key, value, default)
. 无论如何,我看到对
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.
您在64位系统上运行32位进程。 So
HKLM\\Software
is redirected to HKLM\\Software\\Wow6432Node
. 所以
HKLM\\Software
被重定向到HKLM\\Software\\Wow6432Node
。
When the key does not exist, the Registry.GetValue
returns null
rather than the default value and so the exception is thrown. 当密钥不存在时,
Registry.GetValue
返回null
而不是默认值,因此抛出异常。
If the name is not found in the specified key, returns a default value that you provide, or null if the specified key does not exist.
如果在指定的键中找不到该名称,则返回您提供的默认值,如果指定的键不存在,则返回null。
The default value is only returned if the specified key (that is the first parameter, ie location
in your example) is found but a value with the specified name (the second parameter, "Uri"
in your example) does not exist within that key. 如果指定键(也就是第一个参数,即默认值仅返回
location
在你的例子) 被发现 ,但与指定名称(第二个参数的值"Uri"
在你的例子)不认为关键中存在。
If the key itself does not exist a null
reference is returned. 如果密钥本身不存在,则返回
null
引用。
This is fairly well documented on MSDN . 这在MSDN上得到了很好的记录。
Probably the key you are looking for does not exist, probably because you are running on a 64-bit environment now which means that in case your application is a 32-bit process the HKLM\\Software
key gets mapped to HKLM\\Software\\Wow6432Node
. 可能您正在寻找的密钥不存在,可能是因为您现在在64位环境中运行,这意味着如果您的应用程序是32位进程,
HKLM\\Software
密钥将映射到HKLM\\Software\\Wow6432Node
。
I've no idea why but i solved using 我不知道为什么,但我解决了使用
My.Computer.Registry.CurrentUser.GetValue("Software\Mykey","Default Value")
instead of
My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software","Mykey","Default Value")
If the key doesn't exist the first example returns "Default Value", the second a null object! 如果该键不存在,则第一个示例返回“Default Value”,第二个示例返回一个null对象!
I know this is ancient, but in case anybody else stumbles upon this issue, I found an approach that works well for me. 我知道这很古老,但万一其他人偶然发现了这个问题,我找到了一种适合我的方法。 I'm using OpenSubKey, instead of GetValue.
我正在使用OpenSubKey而不是GetValue。
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\MyProduct");
string data = key.GetValue("MyValue") as string;
The registry's terminology caused me some confusion. 注册表的术语让我有些困惑。 It is not simply a key -> value store, it is more like key -> value -> data.
它不仅仅是一个关键 - >值存储,它更像是键 - >值 - >数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.