mar*_*jne 29 .net registry wow64
我的.NET应用程序(任何CPU)需要读取由32位程序创建的注册表值.在64位Windows上,这是在注册表中的Wow6432Node键下.我已经读过你不应该硬编码到Wow6432Node,那么用.NET访问它的正确方法是什么?
woa*_*any 54
如果您可以将目标.Net版本更改为v4,那么您可以使用新的OpenBaseKey函数,例如
RegistryKey registryKey;
if (Environment.Is64BitOperatingSystem == true)
{
registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
}
else
{
registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
}
Run Code Online (Sandbox Code Playgroud)
Jar*_*Par 12
如果您明确需要在64位程序中读取由32位程序写入的值,则可以对其进行硬编码.仅仅因为没有其他选择.
我当然会将它抽象为辅助函数.例如
public RegistryKey GetSoftwareRoot() {
var path = 8 == IntPtr.Size
? @"Software\Wow6432Node"
: @"Software";
return Registry.CurrentUser.OpenSubKey(path);
}
Run Code Online (Sandbox Code Playgroud)
扩展Anders的答案,有一个很好的例子,可以将结果句柄包装在Shahar Prish博客上的.NET RegistryKey对象中 - 请务必阅读这些评论.
请注意,RegOpenKeyEx的pinvoke.net包装器的未经修饰的使用 充满了问题.