C# 读取注册表:ProductID 在 x86 目标应用程序中返回 null.“任何 CPU"工作正常 [英] C# Reading the registry: ProductID returns null in x86 targeted app. "Any CPU" works fine
问题描述
我最近搬到了装有 VS 2010 的 W7 64 位机器上.我的项目设置为在 Any CPU
上运行.当我将其更改为针对 x86
时,我注意到我的一些注册表调用不再有效.
I have recently moved to a W7 64bit machine with VS 2010.
My project is set to run on Any CPU
. When I change this to be targeted at x86
I noticed some of my registry calls no longer work.
我正在尝试像这样读取 ProductID
字段:
I am trying to read the ProductID
field like so:
RegistryKey windowsNTKey = Registry.LocalMachine.OpenSubKey(@"SoftwareMicrosoftWindows NTCurrentVersion");
object productID = windowsNTKey.GetValue("ProductId");
在 x86 模式下运行时,
productID
始终为 null
,在任何 CPU"下运行时,它可以正常工作.这是怎么回事?
productID
is always null
when running in x86 mode, when running in "Any CPU" it works correctly. What is going on here?
推荐答案
某些注册表项被 WOW64 重定向.MSDN 上提供了有关此主题的更多信息http://msdn.microsoft.com/en-us/library/aa384232(v=vs.85).aspx
Some registry keys are redirected by WOW64. More information on this topic is available on MSDN http://msdn.microsoft.com/en-us/library/aa384232(v=vs.85).aspx
如果您确实想始终访问 x64 节点 (.Net4) :
If you really want to always access the x64 node (.Net4) :
RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey windowsNTKey = localMachine.OpenSubKey(@"SoftwareMicrosoftWindows NTCurrentVersion");
object productID = windowsNTKey.GetValue("ProductId");
这篇关于C# 读取注册表:ProductID 在 x86 目标应用程序中返回 null.“任何 CPU"工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!