baa*_*l80 11 c# registry 64-bit
我正在使用64位Windows,我的应用程序以提升的权限运行.我对以下非常简单的代码有疑问:
myKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (myKey != null)
{
string[] HKLMvaluenames = myKey.GetValueNames();
}
Run Code Online (Sandbox Code Playgroud)
但由于某些原因,HKLMvaluenames数组中填充了以下键中的值:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run
Run Code Online (Sandbox Code Playgroud)
有没有解决这个问题的方法?
Han*_*ant 17
这是设计使然,32位程序具有与64位程序不同的注册表视图.当他们尝试从HKLM\Software配置单元读取值时,它们将重定向到HKLM\Software\Wow6432Node键.如果使用Project + Properties,Build选项卡,Platform Target = Any CPU构建C#程序,那么它将作为64位程序运行,不会被重定向.
32位程序可以取消重定向,但使用.NET RegistryKey类并不容易.P /使用KEY_WOW64_64KEY选项调用RegOpenKeyEx是必需的.此Windows SDK文章中提供了更多信息.
编辑:现在也可以使用.NET 4特定的RegistryKey.OpenBaseKey()方法.传递RegistryView.Registry64以64位进程的方式查看注册表.
虽然我遇到了同样的问题或同样关注这个问题的原因,但我找到了一个解决方案.
用这个:
//To set the Base to the right path set RegistryView to DEFAULT, Registry64 or Registry32 regarding the path you need. If you want to go over the x64 root:
RegistryKey rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
Run Code Online (Sandbox Code Playgroud)
现在它会按照你想要的方式行事.
玩得开心!
| 归档时间: |
|
| 查看次数: |
7885 次 |
| 最近记录: |