c# - 无法读取注册表项

标签 c# null registrykey

给出下面的代码,lastuser 字符串返回 null,但是,如果我使用 regedit 查看这个键,它有与之关联的数据。 LoggedOnSAMuser 是受限 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 个可能的问题:

  1. 您正在尝试读取 LoggedOnSAMUser 键,您很有可能 意思是 LastLoggedOnSAMUser
  2. 您可能正在尝试从 32 位应用程序读取 64 位注册表项。如果可能,请将平台目标更改为 x64 并重试。如果不可能,您可能必须直接使用注册表 API。希望在正确的方向上轻推:link

关于c# - 无法读取注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26299421/

相关文章:

python - mysql/python 连接器使用参数和字典在处理空值时死亡

c++-cli - 时间:2019-03-17 标签:c++CLI: get HKEY from SafeRegistryHandle

windows - 使用 PowerShell 从注册表项中检索信息

c# - 我如何比较两个列表并创建另一个匹配的列表?

C# List<T> 与 IEnumerable<T> 性能问题

c# - 如何调试 Azure 中 ASP.NET Web 应用程序的 502 错误?

python - pandas 使用查询函数检查列是否为空

c# - 为什么 ILGenerator 在 Foreach 语句中插入 Leave 指令

bash - 是 shell : command portable?

c++ - 为什么 RegQueryValueEx() 函数在尝试读取注册表项时返回 ERROR_FILE_NOT_FOUND?