What happened to Registry.GetValue?

I am trying to get the registry value:

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

In Windows XP, everything is fine, but in Windows 7 it 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.

How am I wrong? With other values, everything is fine in both OSs.

+8
c # registry
Mar 10 2018-11-11T00:
source share
4 answers

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

Try the following code compilation for both 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 learn more about Accessing an Alternate Registry View .

You can find in here a way to read values ​​in x86 and x64.

+23
Oct. 20 '11 at 10:45
source share

This is probably due to UAC (User Account Control). Extra security for Windows Vista and Windows 7.

You will need to request permissions for the registry.

EDIT : Your code now:

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

Only asks for permission in the cryptography unit, maybe this causes a problem (at least I had it once), so the new code will be as follows:

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

EDIT2:
I attached a debugger to it, according to 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 this particular value, at least my guess, because all the data is correct until I open key3, there the ValueCount is zero, not the expected one.

I think this is a special meaning that is protected.

+7
Mar 10 '11 at 16:44
source share

You say you are on 64-bit Windows: is your application 32-bit? If so, it is likely to affect registry redirection and looks at HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Cryptography . You may need P / Invoke to get around it: http://msdn.microsoft.com/en-us/library/aa384129.aspx .

+6
Mar 10 '11 at 17:03
source share

If you are not an administrator, you only have permission to read HKLM. Instead, you need to open the key read-only. Not sure how to do this with the .NET Registry class; with the API directly, you use RegOpenKeyEx () with the KEY_READ flag.

EDIT: after checking the MSDN, I see that OpenSubKey () only opens read and returns the content if it was successful, and nothing if it crashes. Since you are linking multiple OpenSubKey calls, most likely one of them is not working, which causes the others to fail. Try to break them down into separate calls and check the return intermediate values.

+3
Mar 10 2018-11-11T00:
source share



All Articles