Can't Read Registry Key

若如初见. 提交于 2020-02-04 04:16:32

问题


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);
    }

回答1:


2 possible issues:

  1. You are trying to read the LoggedOnSAMUser key, quite a chance you meant LastLoggedOnSAMUser.
  2. 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



回答2:


Almost certainly you have a 32 bit process on a 64 bit machine and so are subject to registry redirection. Your 32 bit process, by default, reads from the 32 bit view of the registry. But you want to read from the 64 bit view.

Solve the problem by requesting that you read from the 64 bit view of the registry, by way of the RegistryView enumeration.




回答3:


This seems to work on Windows 7

    RegistryKey thisKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    RegistryKey thisSubkey = thisKey.OpenSubKey(@"SOFTWARE\\fred", false);
    _url = (string)thisSubkey.GetValue("_url", "*");
    _port = (string)thisSubkey.GetValue("_port", 0);


来源:https://stackoverflow.com/questions/26299421/cant-read-registry-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!