首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# -登录会话列表

问C# -登录会话列表
EN

Stack Overflow用户
提问于 2009-06-15 06:00:23
回答 1查看 2.9K关注 0票数 0

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

EN

回答 1

Stack Overflow用户

发布于 2009-06-15 06:07:34

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

代码语言:javascript
复制
 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那里得到的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/994652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档