访问使用.NET Windows注册表? [英] Accessing the Windows registry using .NET?

查看:165
本文介绍了访问使用.NET Windows注册表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个奇怪的现象与 .NET 模块访问的 Windows注册表使用RegistryKey类。

I am finding a strange behavior with a .NET module accessing the Windows Registry using the RegistryKey class.

例如,我已经写了.NET模块,testcom.dll,其访问注册表。这testcom.dll文件由一个原生的32位应用程序和64位应用程序同时使用。我的要求是获得REGKEY的值(路径是 HKEY_LOCAL_MACHINE \ SOFTWARE \测试\ MyParameters 键,键名是年龄)。这种年龄的关键将是32位注册表在32位机和64位机64位注册表(不WOW64)。

For example, I have written a .NET module, testcom.dll, which access the registry. This testcom.dll file is used both by a native 32-bit application and a 64-bit application. My requirement is to get the value of a regkey (path being HKEY_LOCAL_MACHINE\SOFTWARE\Test\MyParameters and the key name is Age). This "Age" key will be in 32-bit registry on 32-bit machines and 64-bit registry (not WOW64) on 64-bit machines.

在64位机,32位应用程序正在使用testcom.dll时,关键的时代搜索在WOW64注册表。当64位应用程序正在使用testcom.dll,关键的时代搜索中的64位注册表。

On a 64-bit machine, when a 32-bit application is using testcom.dll, the key "Age" is searched in the WOW64 registry. When a 64-bit application is using testcom.dll, the key "Age" is searched in the 64-bit registry.

我的要求是要读的64位计算机的64位注册表的任何应用程序使用testcom.dll文件的密钥。我怎样才能做到这一点?

My requirement is to read the key in the 64-bit registry on 64-bit machines whatever the application uses the testcom.dll file. How can I do this?

推荐答案

如果你可以改变目标.NET版本为V4,那么你可以使用新的OpenBaseKey的功能,例如

If you can change the target .Net version to v4, then you can use the new OpenBaseKey function e.g.

RegistryKey registryKey;
if (Environment.Is64BitOperatingSystem == true)
{
    registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
}
else
{
    registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
}

这篇关于访问使用.NET Windows注册表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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