获取计算机的用户

发布于 2024-09-10 17:42:10 字数 798 浏览 3 评论 0原文

我正在尝试使用以下代码获取计算机的本地用户列表。

       internal void GetUsers()
       {
        try
        {
            List<string> adUsers = new List<string>();
            DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.MachineName);

            foreach (DirectoryEntry child in directoryEntry.Children)
            {
                if (child.SchemaClassName.Equals("User", StringComparison.OrdinalIgnoreCase))
                {
                    adUsers.Add(child.Name);
                }
            }
        }
        catch (Exception ex)
        {
            //Exception
        }
    }

这段代码在我的电脑上运行良好。然而,当我在其他几台计算机上进行测试时,列表中包含以下系统用户:

ASPNET、 HelpAssistant

有人可以告诉我如何摆脱这些系统用户并只获取实际登录的用户,即普通用户。

谢谢, 内存

I am trying to get the list of local users of a computer using the following code.

       internal void GetUsers()
       {
        try
        {
            List<string> adUsers = new List<string>();
            DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.MachineName);

            foreach (DirectoryEntry child in directoryEntry.Children)
            {
                if (child.SchemaClassName.Equals("User", StringComparison.OrdinalIgnoreCase))
                {
                    adUsers.Add(child.Name);
                }
            }
        }
        catch (Exception ex)
        {
            //Exception
        }
    }

This code works fine in my computer. However, when I tested it on a few other computers, the following system users were included in the list:

ASPNET,
HelpAssistant

Could some one throw some light on how I can get rid of these system users and get only users who actually log in, ie, normal users.

Thanks,
Ram

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独孤求败 2024-09-17 17:42:10

不是一个答案,而是一些可能有帮助的建议。

我认为问题在于这些帐户不是真正的系统帐户,因此可能不太容易区分。

您可以查看 WMI 类 Win32_UserAccountWin32_UserProfile 并查看是否有其中的属性可能表明哪些用户帐户是普通帐户以及哪些是您提到的帐户。具体来说,可能是 Win32_UserAccount 的“SIDType”或“AccountType”属性,也可能是 Win32_UserProfile 类的 Special 属性。

可能还有其他值得关注的 WMI 类。

或者可能有某种方法可以查询用户帐户是否具有交互式登录权限(我假设这两个帐户通常可能没有)。

Not an answer as such, but some suggestions that might help.

I think the problem is that those accounts aren't real system accounts, so might not be so easy to distinguish.

You could look at the WMI classes Win32_UserAccount and Win32_UserProfile and see if there are any properties in there that might indicate which user accounts are normal ones and which ones are the ones you mention. Specifically, maybe the 'SIDType' or 'AccountType' properties of Win32_UserAccount or maybe the Special property of the Win32_UserProfile class.

Might be other WMI classes that might be worth looking at as well.

Or there might be some way that you can query if a user account has the interactive logon right (which I assume those two accounts might not have normally).

夏末染殇 2024-09-17 17:42:10

您是否尝试过枚举 DirectoryEntry 上的 Properties 集合?

    using (DirectoryEntry dirEntry = new DirectoryEntry(strchild))
    {
        foreach (string strPropertyName in dirEntry.Properties.PropertyNames)
        {
            Console.WriteLine(strPropertyName + " " + dirEntry.Properties[strPropertyName].Value.ToString());
        }
    }

除此之外,您可能需要在 Active Directory 上执行 LDAP 搜索,以将您找到的用户名与 ActiveDirectory 用户相匹配。
看看这篇文章。
http://www.codeproject.com/KB/system/everythingInAD.aspx

玩得开心。

Have you tried enumerating the Properties collection on DirectoryEntry?

    using (DirectoryEntry dirEntry = new DirectoryEntry(strchild))
    {
        foreach (string strPropertyName in dirEntry.Properties.PropertyNames)
        {
            Console.WriteLine(strPropertyName + " " + dirEntry.Properties[strPropertyName].Value.ToString());
        }
    }

Other than that, you may have to do an LDAP search on Active Directory to match the UserName you have found to an ActiveDirectory user.
Have a look at this article.
http://www.codeproject.com/KB/system/everythingInAD.aspx

Have fun.

把昨日还给我 2024-09-17 17:42:10

以下代码将为您提供实际拥有本地可访问文件夹的本地用户。

var localDrives = Environment.GetLogicalDrives();
var localUsers = new List<string>();
var query = new SelectQuery("Win32_UserAccount") { Condition = "SIDType = 1 AND AccountType = 512" };
var searcher = new ManagementObjectSearcher(query);

foreach (ManagementObject envVar in searcher.Get())
{
    foreach (string drive in localDrives)
    {
        var dir = Path.Combine(String.Format("{0}Users", drive), envVar["name"].ToString());
        if (Directory.Exists(dir))
        {
            localUsers.Add(envVar["name"].ToString());
        }
    }
}

The following code will get you the local users that actually have local accessible folders.

var localDrives = Environment.GetLogicalDrives();
var localUsers = new List<string>();
var query = new SelectQuery("Win32_UserAccount") { Condition = "SIDType = 1 AND AccountType = 512" };
var searcher = new ManagementObjectSearcher(query);

foreach (ManagementObject envVar in searcher.Get())
{
    foreach (string drive in localDrives)
    {
        var dir = Path.Combine(String.Format("{0}Users", drive), envVar["name"].ToString());
        if (Directory.Exists(dir))
        {
            localUsers.Add(envVar["name"].ToString());
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文