C# 读取注册表 : ProductID returns null in x86 targeted app. "Any CPU"工作正常

标签 c# visual-studio-2010 registry 32bit-64bit

我最近搬到了一台装有 VS 2010 的 W7 64 位机器上。 我的项目设置为在 Any CPU 上运行。当我将其更改为针对 x86 时,我注意到我的某些注册表调用不再有效。

我正在尝试像这样读取 ProductID 字段:

RegistryKey windowsNTKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion");
object productID = windowsNTKey.GetValue("ProductId");

productID 在 x86 模式下运行时始终为 null,在“任何 CPU”下运行时它可以正常工作。这是怎么回事?

最佳答案

一些注册表项被 WOW64 重定向。有关此主题的更多信息,请访问 MSDN http://msdn.microsoft.com/en-us/library/aa384232(v=vs.85).aspx

如果您真的想始终访问 x64 节点 (.Net4):

  RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
  RegistryKey windowsNTKey = localMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion");
  object productID = windowsNTKey.GetValue("ProductId");

关于C# 读取注册表 : ProductID returns null in x86 targeted app. "Any CPU"工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6304275/

相关文章:

android - .axml 突出显示和智能感知在 monodroid 中不起作用

windows - Windows 将其 "Open With"设置存储在哪里?

c# - 什么是白标 dll 的好方法

c# - WPF MVVM,按钮单击时递增和递减 Datagrid 单元格值

c# - 在可移植类库中使用 SQLite

c# - 上午和下午 12 小时的时间正则表达式

javascript - Visual Studio 2010 : Javascript Highlighting

nginx - docker pull push 登录成功后无法正常工作

c++ - 获取 Windows ID C++

c# - 当前位置没有可用的资源