Can't read certain registry keys programmatically
I have a little app which reads registry key string values. It works well but for some reason it fails on this key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductId
Despite working on other values of HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\
It also fails on `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid'
I am running as an admin, is this a factor? I'm running W7 64bit, another W7 machine and Vista machine both work fine. My only guesses are some permissioning issue, or related to me running 64-bit.
update:
It appears to be something to do with my system running Windows 64bit, and \Software\Wow6432Node\
. I don't know what that is though. I have both \Software\Wow6432Node\Microsoft\Windows NT\CurrentVersion\
and \Software\Microsoft\Windows NT\C开发者_JAVA技巧urrentVersion\
but only the latter contains ProductId value... for some reason when I ask for the key Windows is apparently looking in the Wow6432Node
We're using wxWidgets but could probably use some win32 code directly if needed... our app is a 32bit application but target PCs could be running 32 or 64 bit versions of Windows
It's due to WOW64. This other question focuses on the details.
I got the following code to work on a 32 bit XP box and a 64 bit Win 7 box. I think that should cover most bases.
// start out trying to read machine guid on 32 bit machine
object value = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", @"MachineGuid", (object) "defaultValue");
if (value != null && value.ToString() != "defaultValue")
{
return value.ToString();
}
// read machine guid on 64 bit machine
RegistryKey regKeyBase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey regKey = regKeyBase.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography", RegistryKeyPermissionCheck.ReadSubTree);
value = regKey.GetValue("MachineGuid", (object) "defaultValue");
regKeyBase.Close();
regKey.Close();
if (value != null && value.ToString() != "defaultValue")
{
return value.ToString();
}
return string.Empty;
I just looked at my registry and there's no key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductId or MachineGuid, this is on Win7 64-bit
精彩评论