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

查看:37
本文介绍了使用 .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_MACHINESOFTWARETestMyParameters,密钥名称为 Age).这个年龄"键将在 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_MACHINESOFTWARETestMyParameters 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 注册表中搜索键Age".当 64 位应用程序使用 testcom.dll 时,会在 64 位注册表中搜索键Age".

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天全站免登陆