避免使用注册表Wow6432Node重定向

我试图在c#中使用Microsoft.Win32.RegistryKey插入一些简单的注册表项,但路径会自动更改为:

HKEY_LOCAL_MACHINE\SOFTWARE\Test 

 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test 

我试过谷歌,但我只得到一些模糊和混乱的结果。 有没有人以前处理过这个问题? 一些示例代码将得到很多赞赏。

您可以使用RegistryKey.OpenBaseKey来解决此问题:

 var baseReg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); var reg = baseReg.CreateSubKey("Software\\Test"); 

在WOW64下,某些注册表项被重定向(SOFTWARE)。 当32位或64位应用程序对重定向的密钥进行注册表调用时,注册表重定向器会拦截该调用并将其映射到密钥的相应物理注册表位置。 有关更多信息,请参阅注册表重定向器 。

您可以使用RegistryKey.OpenBaseKey方法上的RegistryView枚举显式打开32位视图并直接访问HKLM \ Software \。

我不知道如何使用.reg文件解决它。 但仅在BAT文件中,如下所示:

您必须在命令行的末尾添加/reg:64 。 例如:

 REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background" /v "OEMBackground" /t REG_DWORD /d 0x00000001 /f /reg:64 

来源: Wow6432Node以及如何通过Sccm将注册表设置部署到64位系统

这是我开发的工作代码,只读取和写入32位注册表。 它适用于32位和64位应用程序。 如果没有设置值,’read’调用会更新注册表,但是如何删除它是非常明显的。 它需要.Net 4.0,并使用OpenBaseKey / OpenSubKey方法。

我目前使用它来允许64位后台服务和32位托盘应用程序无缝访问相同的注册表项。

 using Microsoft.Win32; namespace SimpleSettings { public class Settings { private static string RegistrySubKey = @"SOFTWARE\BlahCompany\BlahApp"; public static void write(string setting, string value) { using (RegistryKey registryView = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)) using (RegistryKey registryCreate = registryView.CreateSubKey(RegistrySubKey)) using (RegistryKey registryKey = registryView.OpenSubKey(RegistrySubKey, true)) { registryKey.SetValue(setting, value, RegistryValueKind.String); } } public static string read(string setting, string def) { string output = string.Empty; using (RegistryKey registryView = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)) using (RegistryKey registryCreate = registryView.CreateSubKey(RegistrySubKey)) using (RegistryKey registryKey = registryView.OpenSubKey(RegistrySubKey, false)) { // Read the registry, but if it is blank, update the registry and return the default. output = (string)registryKey.GetValue(setting, string.Empty); if (string.IsNullOrWhiteSpace(output)) { output = def; write(setting, def); } } return output; } } } 

用法:将它放在它自己的类文件(.cs)中并按如下方式调用它:

 using SimpleSettings; string mysetting = Settings.read("SETTINGNAME","DEFAULTVALUE");