tags:

views:

79

answers:

2

Hi,

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

+2  A: 

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

ho1
Hi ho1,thanks a lot for your reply... I shall try that...
Ram
+1  A: 

Hey Ram,
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.

blorkfish
Hi blorkfish,I suspected that the properties would help. But havent tried it out.. Will try and get back.. Thanks for your reply.
Ram