我想知道如何检索登录的用户列表。我可以打开Windows任务管理器,查看“用户”选项卡,查看PC上登录的用户。我如何获得该用户列表。感谢。
答案 0 :(得分:1)
看看WMI。您可以使用System.Management命名空间:
System.Management.ConnectionOptions myConnectionOptions = new System.Management.ConnectionOptions();
myConnectionOptions.Impersonation = System.Management.ImpersonationLevel.Impersonate;
System.Management.ManagementScope objwmiservice;
System.Management.ManagementObjectSearcher myObjectSearcher;
System.Management.ManagementObjectCollection myCollection;
try
{
objwmiservice = new System.Management.ManagementScope( ("\\\\" + (HostOrIP + "\\root\\cimv2")), myConnectionOptions);
objwmiservice.Connect();
myObjectSearcher = new System.Management.ManagementObjectSearcher(objwmiservice.Path.ToString(), "Select UserName from Win32_ComputerSystem");
myObjectSearcher.Options.Timeout = new TimeSpan(0, 0, 0, 0, 7000);
myCollection = myObjectSearcher.Get();
foreach (System.Management.ManagementObject myObject in myCollection)
{
if (!(myObject.GetPropertyValue("User name") == null))
{
string Userx = myObject.GetPropertyValue("Usernam e").ToString();
int posx = Userx.LastIndexOf("\\");
if ((posx > 0))
{
Userx = Userx.Substring((posx + 1));
return Userx.ToUpper();
}
}
}
return "<Nobody>";
}
catch (Exception)
{
return "<Nobody>";
}
}
我从this link获得了