Registry.GetValue 有什么问题?

发布于 2024-10-21 09:51:38 字数 569 浏览 1 评论 0原文

我试图获取注册表值:

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

在 Windows XP 中一切正常,但在 Windows 7 中返回 0。在使用 regedit 的 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography 中,我看到 MachineGuid,但是如果我运行

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

keys.Length 为 0。

我做错了什么?其他值在两个操作系统中都可以。

I trying to get a registry value:

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

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();

keys.Length is 0.

What do I do wrong? With other values all ok in both of OS.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谜兔 2024-10-28 09:51:38

问题是您可能正在将解决方案编译为 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;
        }
    }
}

您可以阅读有关访问备用注册表视图的更多信息。

您可以在 这里是一种读取 x86 和 x64 值的方法。

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

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;
        }
    }
}

You can read more about Accessing an Alternate Registry View.

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

新一帅帅 2024-10-28 09:51:38

这可能与UAC(用户帐户控制)有关。 Windows Vista 和 Windows 7 的额外保护层。

您需要向注册表请求权限。

编辑
您现在的代码:

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

仅请求加密子项的权限,也许这会导致问题(至少我遇到过一次),因此新代码将是:

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

EDIT2:
我在这段代码上附加了调试器:

var key1 = Registry.LocalMachine.OpenSubKey("SOFTWARE", RegistryKeyPermissionCheck.ReadSubTree);
var key2 = key1.OpenSubKey("Microsoft", RegistryKeyPermissionCheck.ReadSubTree);
var key3 = key2.OpenSubKey("Cryptography", RegistryKeyPermissionCheck.ReadSubTree);
var key4 = key3.GetValueNames();

事实证明,您可以读取该特定值,至少这是我的猜测,因为所有数据都是正确的,直到我打开 key3,ValueCount 为零,而不是预期的 1我

认为这是受保护的特殊价值。

It probably has to do with UAC (User Account Control). The extra layer of protection for Windows Vista and Windows 7.

You'll need to request permissions to the registry.

EDIT:
Your code right now:

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

Only requests the permissions on the Cryptography subkey, maybe that causes the problem (at least I had that once), so the new code would then be:

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

EDIT2:
I attached the debugger to it, on this code:

var key1 = Registry.LocalMachine.OpenSubKey("SOFTWARE", RegistryKeyPermissionCheck.ReadSubTree);
var key2 = key1.OpenSubKey("Microsoft", RegistryKeyPermissionCheck.ReadSubTree);
var key3 = key2.OpenSubKey("Cryptography", RegistryKeyPermissionCheck.ReadSubTree);
var key4 = key3.GetValueNames();

It turns out, you can read that specific value, at least that's my guess, because all data is correct, until I open key3, there the ValueCount is zero, instead of the expected 1.

I think it's a special value that's protected.

短暂陪伴 2024-10-28 09:51:38

您说您使用的是 64 位 Windows:您的应用程序是 32 位吗?如果是这样,它可能受到注册表重定向的影响,并且正在查看HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Cryptography。您可能需要 P/Invoke 才能解决此问题:http://msdn.microsoft .com/en-us/library/aa384129.aspx

You say you're on 64-bit Windows: is your app 32-bit? If so it's probably being affected by registry redirection and is looking at HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Cryptography. You may have to P/Invoke to work around it: http://msdn.microsoft.com/en-us/library/aa384129.aspx.

离线来电— 2024-10-28 09:51:38

如果您不是管理员,您只有 HKLM 的读取权限。您需要以只读方式打开密钥。不知道如何使用 .NET 的注册表类来做到这一点;直接使用 API,您可以使用 RegOpenKeyEx() 带有 KEY_READ 标志。

编辑:检查 MSDN 后,我看到 OpenSubKey() 是只读打开,成功则返回内容,失败则不返回任何内容。由于您要链接多个 OpenSubKey 调用,因此很可能其中一个调用失败,从而导致其他调用失败。尝试将它们分解为单独的调用,并检查返回的中间值。

If you're not an administrator, you only have read permission on HKLM. You need to open the key read-only instead. Not sure how to do that with .NET's Registry class; with the API directly, you use RegOpenKeyEx() with the KEY_READ flag.

EDIT: After checking MSDN, I see that OpenSubKey() does open read only, and returns the contents if it succeeds and nothing if it fails. Since you're chaining multiple OpenSubKey calls, it's most likely one of them that's failing that causes the others to fail. Try breaking them out into separate calls, and checking the intermediate values returned.

凑诗 2024-10-28 09:51:38

也许聚会有点晚了,但是,没有一个解决方案对我有用。

这就是我解决这个问题的方法:

public static Guid GetMachineGuid
    {
        get
        {
            var machineGuid = Guid.Empty;

            var localMachineX64View = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            var cryptographySubKey = localMachineX64View.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography");
            
            if (cryptographySubKey == null) return machineGuid;
            
            var machineGuidValue = (string)cryptographySubKey.GetValue("MachineGuid");

            Guid.TryParse(machineGuidValue, out machineGuid);

            return machineGuid;
        }
    }

Maybe a little late to the party, but, none of the solutions worked for me.

This is how I've solved this issue:

public static Guid GetMachineGuid
    {
        get
        {
            var machineGuid = Guid.Empty;

            var localMachineX64View = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            var cryptographySubKey = localMachineX64View.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography");
            
            if (cryptographySubKey == null) return machineGuid;
            
            var machineGuidValue = (string)cryptographySubKey.GetValue("MachineGuid");

            Guid.TryParse(machineGuidValue, out machineGuid);

            return machineGuid;
        }
    }
执妄 2024-10-28 09:51:38

当我导入 Microsoft.Win32 并将应用程序设置更改为 x64(如 pedrocgsousa 提到的)时,我解决了问题。

I solved the problem when i imported Microsoft.Win32 and changed the application-settings to x64 like pedrocgsousa mentioned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文