Registry.GetValue怎么了? [英] What's wrong with Registry.GetValue?

查看:158
本文介绍了Registry.GetValue怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取注册表值:

var value = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGuid", 0);

在Windows XP中一切正常,但在Windows 7中返回0。在 HKEY_LOCAL_MACHINE软件Microsoft\密码学使用regedit我看到 MachineGuid ,但是如果我运行

In Windows XP all ok, but in Windows 7 returns 0. In HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography using regedit I see MachineGuid, but if I run

var keys = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Cryptography", RegistryKeyPermissionCheck.ReadSubTree).GetValueNames();

键。长度为0。

我做错了什么?

推荐答案

问题是,如果您将解决方案编译为x86,则问题是

The problem is that you probably are compiling the solution as x86, if you compile as x64 you can read the values.

尝试将以下代码编译为x86和x64:

Try the following code compiling as x86 and 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;
        }
    }
}

您可以阅读有关< a href = http://msdn.microsoft.com/en-us/library/aa384129.aspx>访问备用注册表视图。

您可以在此处一种在x86和x64中读取值的方法。

You can found in here a way of reading values in x86 and x64.

这篇关于Registry.GetValue怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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