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

查看:268
本文介绍了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(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
    if (myKey != null)
    {
    string[] HKLMvaluenames = myKey.GetValueNames();
    }

但是出于某些原因,HKLMvaluenames数组中填充了以下键中的值:

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

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run

有解决此问题的方法吗?

Is there a way around this problem?

推荐答案

这是设计使然,与64位程序相比,32位程序具有不同的注册表视图。当他们尝试从HKLM\Software配置单元读取值时,它们将重定向到HKLM\Software\Wow6432Node密钥。如果您使用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 HKLM\Software\Wow6432Node key when they try to read a value from the HKLM\Software 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 /使用KEY_WOW64_64KEY选项调用RegOpenKeyEx是必需的。有关更多信息,请参见 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也可以使用.NET 4特定的RegistryKey.OpenBaseKey()方法使用它。传递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天全站免登陆