C# 避免注册表 Wow6432Node 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11808462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:20:45  来源:igfitidea点击:

Avoid Registry Wow6432Node Redirection

c#registrywow64

提问by

I'm trying to insert some simple registry keys using Microsoft.Win32.RegistryKey in c# but the path automatically changes from:

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

HKEY_LOCAL_MACHINE\SOFTWARE\Test

to

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test

I tried google but I only get some vague and confusing results. Has anyone dealt with this issue before? Some example code would be much appereciated.

我试过谷歌,但我只得到一些模糊和混乱的结果。以前有人处理过这个问题吗?一些示例代码会很受欢迎。

采纳答案by Hui

You can use RegistryKey.OpenBaseKey to solve this problem:

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

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

回答by Jacob Seleznev

Under WOW64, certain registry keys are redirected (SOFTWARE). When a 32-bit or 64-bit application makes a registry call for a redirected key, the registry redirector intercepts the call and maps it to the key's corresponding physical registry location. For more information, see Registry Redirector.

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

You can use the RegistryView Enumerationon RegistryKey.OpenBaseKey Methodto open the 32-bit view explicitly and access HKLM\Software\ directly.

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

回答by Pedro77

I don't know how to solve it using a .reg file. But only in a BAT file, as follow:

我不知道如何使用 .reg 文件解决它。但仅限于BAT文件,如下:

You must add /reg:64at the end of the command line. ex:

您必须/reg:64在命令行末尾添加。前任:

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

Source: Wow6432Node and how to Deploy Registry settings to 64 bit systems via Sccm

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

回答by Miles Prower

Here is the working code I have developed to both read and write ONLY the 32-bit registry. It works in both 32-bit and 64-bit applications. The 'read' call updates the registry if the value is not set, but it is very obvious how to remove that. It requires .Net 4.0, and uses the OpenBaseKey/OpenSubKey methods.

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

I currently use it to allow a 64-bit background service and a 32-bit tray application to access the same registry keys seamlessly.

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

Usage: Put this in it's own class file (.cs) and call it as such:

用法:把它放在它自己的类文件 (.cs) 中并这样调用它:

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