C# C-登录会话列表
我想知道如何检索已登录用户的列表。我可以打开Windows任务管理器,查看“用户”选项卡,查看登录到电脑上的用户。如何获取用户列表。谢谢。看看WMI。您可以使用System.Management命名空间:C# C-登录会话列表,c#,.net,C#,.net,我想知道如何检索已登录用户的列表。我可以打开Windows任务管理器,查看“用户”选项卡,查看登录到电脑上的用户。如何获取用户列表。谢谢。看看WMI。您可以使用System.Management命名空间: System.Management.ConnectionOptions myConnectionOptions = new System.Management.ConnectionOptions(); myConnectionOptions.Impersonation =
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>";
}
}
我从那里得到的,谢谢。当我使用快速用户切换并有多个登录时,这只返回一个用户,这是活动的。当在Windows 2019服务器上本地运行时,它不返回任何内容。不幸的是,这可能是WMI没有在用户名字段中返回任何内容的结果,但它仍然不起作用。好的,很高兴知道这一点。顺便说一句,这是一个11年前的答案。