如何在任务管理器中获取列表用户

时间:2017-05-15 13:50:12

标签: c# .net taskmanager

如何在任务管理器中获取具有状态的列表用户?

我只找到了如何获取域用户列表

var usersSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount");
var users = usersSearcher.Get();

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码获取用户列表:

var usersSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_UserAccount");
var managementObjects = usersSearcher.Get();

List<string> result = new List<string>();

foreach (ManagementObject item in managementObjects)
{
    foreach (var pr in item.Properties)
    {
        if (pr.Name == "Caption")
        {
            result.Add(pr.Value?.ToString());
        }
    }
}

var users = result.Distinct().ToList();

你也可以试试这个:

var usersSearcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Process");
var managementObjects = usersSearcher.Get();
List<string> allUsers = new List<string>();

foreach (ManagementObject obj in managementObjects)
{
    string[] argList = new string[] { string.Empty, string.Empty };
    int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
    if (returnVal == 0)
    {
        // return DOMAIN\user
        allUsers.Add(argList[1] + "\\" + argList[0]);
    }
}

var result = allUsers.Distinct().ToList();