What's wrong with Registry.GetValue?

后端 未结 5 1309
轻奢々
轻奢々 2021-01-12 07:25

I trying to get a registry value:

var value = Registry.GetValue(@\"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography\", \"MachineGuid\", 0);
5条回答
  •  粉色の甜心
    2021-01-12 08:00

    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.

提交回复
热议问题