避免注册表Wow6432节点重定向

本文关键字:节点 重定向 Wow6432 注册表 | 更新日期: 2024-10-24 08:33:27

我正在尝试使用Microsoft插入一些简单的注册表项。Win32.RegistryKey在c#中,但路径自动从更改

HKEY_LOCAL_MACHINE'SOFTWARE'Test

HKEY_LOCAL_MACHINE'SOFTWARE'Wow6432Node'Test

我试过谷歌,但只得到一些模糊和令人困惑的结果。以前有人处理过这个问题吗?一些示例代码会被理解得更多。

避免注册表Wow6432节点重定向

您可以使用RegistryKey。OpenBaseKey解决这个问题:

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

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

可以对RegistryKey使用RegistryView枚举。OpenBaseKey方法显式打开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/OOpenSubKey方法。

我目前使用它来允许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");