我想知道如何检索已登录的用户列表。我可以打开Windows任务管理器,查看用户选项卡并查看登录到PC上的用户。如何获取用户列表。谢谢。
发布于 2009-06-15 06:07:34
看一看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那里得到的
https://stackoverflow.com/questions/994652
复制相似问题