我试图获取注册表值:
var value = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGuid", 0);
在 Windows XP 中一切正常,但在 Windows 7 中返回 0。在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography
中使用 regedit 我看到 MachineGuid
,但如果我运行
var keys = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Cryptography", RegistryKeyPermissionCheck.ReadSubTree).GetValueNames();
keys.Length
为 0。
我做错了什么?使用其他值在两个操作系统中都可以。
最佳答案
问题是您可能将解决方案编译为 x86,如果您编译为 x64,则可以读取值。
尝试将以下代码编译为 x86 和 x64:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("MachineGUID:" + MachineGUID);
Console.ReadKey();
}
public static string MachineGUID
{
get
{
Guid guidMachineGUID;
if (Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography") != null)
{
if (Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography").GetValue("MachineGuid") != null)
{
guidMachineGUID = new Guid(Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Cryptography").GetValue("MachineGuid").ToString());
return guidMachineGUID.ToString();
}
}
return null;
}
}
}
您可以阅读更多关于 Accessing an Alternate Registry View 的信息.
您可以在 here 中找到一种在 x86 和 x64 中读取值的方法。
关于c# - Registry.GetValue 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5262830/