使用.Net MVC2中的C#访问远程注册表项

我正在使用以下代码

private static string GetLogonFromMachine(string machine) { //1. To read the registry key that stores this value. //HKEY_Local_Machine\Software\Microsoft\Windows NT\CurrentVersion\WinLogon\DefaultUserName var rHive = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machine); var rKey = rHive.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\WinLogon"); var rItem = rKey.GetValue("DefaultUserName"); return rItem.ToString(); } 

并且我已经确认我的用户具有访问权限,MVC站点正在使用集成身份validation,并且列出的REG_SZ“DefaultUserName”在目标计算机上具有值,但是rItem没有获取值。

我想我做的事情很傻,我很想知道什么!

我确实很傻。 我在使用之前没有对机器名称列表进行排序,因此我正在查看错误机器的注册表。 实际上处于焦点的机器正确地返回“”。

我结束了

  private static string GetLogonFromMachine(string machine) { //1. To read the registry key that stores this value. //HKEY_Local_Machine\Software\Microsoft\Windows NT\CurrentVersion\WinLogon\DefaultUserName RegistryKey rHive; try { rHive = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machine); } catch (IOException) { return "offline"; } var rKey = rHive.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon\\"); if (rKey == null) { return "No Logon Found"; } var rItem = rKey.GetValue("DefaultUserName"); return rItem==null ? "No Logon Found" : rItem.ToString(); }