C# - 登录会话列表 [英] C# - List of Logon Sessions

查看:117
本文介绍了C# - 登录会话列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何检索登录的用户列表。我可以打开Windows任务管理器,并期待在用户选项卡,并查看了记录的PC上的用户。我如何获取用户的列表。谢谢你。

I would like to know how to retrieve the list of users that are logged on. I can open up Windows Task Manager and look at the Users tab and look at the users that are logged on the PC. How do I get that list of users. Thanks.

推荐答案

看看WMI。您可以使用System.Management命名空间:

Take a look at WMI. You can use System.Management namespace:

 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>";
     }
}

这是我从此链接

这篇关于C# - 登录会话列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆