31

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

HKEY_LOCAL_MACHINE\SOFTWARE\Test

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test

我尝试了谷歌,但我只得到一些模糊和令人困惑的结果。以前有人处理过这个问题吗?一些示例代码将非常受欢迎。

4

4 回答 4

31

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

var baseReg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
var reg = baseReg.CreateSubKey("Software\\Test");
于 2014-04-02T23:51:08.190 回答
9

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

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

于 2012-08-04T13:03:25.757 回答
9

我不知道如何使用 .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 位系统

于 2013-10-04T13:06:54.017 回答
-1

这是我开发的仅读取和写入 32 位注册表的工作代码。它适用于 32 位和 64 位应用程序。如果未设置该值,则“读取”调用会更新注册表,但如何删除它是非常明显的。它需要 .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");
于 2017-05-16T21:38:50.890 回答