C# 在 64 位操作系统上读取错误的注册表数据 [英] C# reads wrong registry data on 64-bit OS

查看:27
本文介绍了C# 在 64 位操作系统上读取错误的注册表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 64 位 Windows 上工作,我的应用程序以提升的权限运行.我对以下非常简单的代码有疑问:

I'm working on a 64-bit Windows and my applicaiton runs with elevated privileges. I have a problem with the following very simple piece of code:

myKey = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
    if (myKey != null)
    {
    string[] HKLMvaluenames = myKey.GetValueNames();
    }

但由于某种原因 HKLMvaluenames 数组填充了来自以下键的值:

But for some reason HKLMvaluenames array is populated with values from the following key:

HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionRun

有没有办法解决这个问题?

Is there a way around this problem?

推荐答案

这是设计使然,32 位程序的注册表视图与 64 位程序不同.当它们尝试从 HKLMSoftware hive 读取值时,它们被重定向到 HKLMSoftwareWow6432Node 键.如果您使用 Project + Properties、Build 选项卡、Platform Target = Any CPU 构建 C# 程序,那么它将作为 64 位程序运行并且不会被重定向.

This is by design, 32-bit programs have a different view of the registry than 64-bit programs. They are redirected to the HKLMSoftwareWow6432Node key when they try to read a value from the HKLMSoftware hive. If you build your C# program with Project + Properties, Build tab, Platform Target = Any CPU then it will run as a 64-bit program and won't get redirected.

32 位程序可以取消重定向,但使用 .NET RegistryKey 类并不容易做到.P/Invoking RegOpenKeyEx 与 KEY_WOW64_64KEY 选项是必需的.此Windows SDK 文章中提供了更多信息.

32-bit programs can cancel the redirection but that's not easily done with the .NET RegistryKey class. P/Invoking RegOpenKeyEx with the KEY_WOW64_64KEY option is required. More info is available in this Windows SDK article.

现在也可通过 .NET 4 特定的 RegistryKey.OpenBaseKey() 方法对 .NET 使用.通过 RegistryView.Registry64 以 64 位进程的方式查看注册表.

this is now also available to .NET with the .NET 4 specific RegistryKey.OpenBaseKey() method. Pass RegistryView.Registry64 to view the registry the way a 64-bit process would.

这篇关于C# 在 64 位操作系统上读取错误的注册表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆