无法读取注册表项
问题描述:
给出以下代码,lastuser字符串返回null,但是,如果我使用regedit查看此键,则它具有与之关联的数据. LoggedOnSAMuser是受限制的密钥吗?
give the code below, lastuser string returns null, however, if I use regedit to look at this key it has data associated with it. Is LoggedOnSAMuser a restricted key?
public static string lastlogon()
{
string lastuser;
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI",false);
if (registryKey != null)
{
lastuser = (string) registryKey.GetValue("LastLoggedOnSAMUser");
}
else lastuser = "Unknown User";
return (lastuser);
}
答
2个可能的问题:
- 您正在尝试阅读
LoggedOnSAMUser
键,您很有可能会 表示LastLoggedOnSAMUser
. - 您可能正在尝试从32位应用程序读取64位注册表项.如果可能,将平台目标更改为x64并重试.如果不可能,则可能必须直接使用注册表API.希望能在正确的方向上进行微调:链接
- You are trying to read the
LoggedOnSAMUser
key, quite a chance you meantLastLoggedOnSAMUser
. - You might be trying to read a 64-bit registry entry from a 32-bit application. If possible, change your platform target to x64 and retry. If not possible, you might have to use the registry API directly. Hopefully a nudge in the right directon: link