Reputation: 6527
Can't open registry subkey even if I try to open in both - x64 and x32 registry. I'm trying to get username of logged in visual studio user.
@"Software\Microsoft\VisualStudio\12.0\ConnectedUser\IdeUser\Cache"
Can't see IdeUser, but Cache returns NULL.
const string SubKey = @"Software\Microsoft\VisualStudio\12.0\ConnectedUser\IdeUser\Cache";
const string EmailAddressKeyName = "EmailAddress";
const string UserNameKeyName = "DisplayName";
RegistryKey localMachineRegistry64 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
RegistryKey reg64 = localMachineRegistry64.OpenSubKey(SubKey, false);
if (reg64 != null)
{
return reg64.GetValue(SubKey, true).ToString();
}
//Check the 32-bit registry for "HKEY_LOCAL_MACHINE\SOFTWARE" if not found in the 64-bit registry:
RegistryKey localMachineRegistry32 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
RegistryKey reg32 = localMachineRegistry32.OpenSubKey(SubKey, false);
if (reg32 != null)
{
return reg32.GetValue(SubKey, true).ToString();
}
Upvotes: 2
Views: 1701
Reputation: 951
I have tested you code and it works as I would expect (to a point). I can access the SubKey. *Note I am running VS as administrator. (right click on the VS exe and run as admin, I am also a local admin my dev machine, so I have un-restricted registry access).
However your code is a little interesting as your not actually accessing the values in the registry by calling GetValue(SubKey,true)
, in my example I altered accordingly.
const string SubKey = @"Software\Microsoft\VisualStudio\12.0\ConnectedUser\IdeUser\Cache";
const string EmailAddressKeyName = "EmailAddress";
const string UserNameKeyName = "DisplayName";
RegistryKey localMachineRegistry64 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
RegistryKey reg64 = localMachineRegistry64.OpenSubKey(SubKey, false);
if (reg64 != null)
{
//******** This is just an example of getting the user name and email.
//get user name and email from the selected sub key.
var userName = reg64.GetValue(UserNameKeyName, String.Empty).ToString();
var emailAddress = reg64.GetValue(EmailAddressKeyName, String.Empty).ToString();
//return a bool and pass back the userName and email as out parms?
return true;
}
Hope this helps.
EDIT -
If registry access continues to be an issue you can monitor registry access via ProcMon from sysinternals.
Upvotes: 1