开发者

Getting users of a computer

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);
                }
            }
      开发者_如何学Python  }
        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


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).


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.


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());
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜