Monday, September 10, 2007

How to get list of windows user in C#

If you want to have the list of an specific windows group in C# you can get this list by these lines of code below. Notice that you have to add a reference to System.DirectoryServices .

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry admGroup = localMachine.Children.Find("administrators","group");
object members = admGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
    DirectoryEntry member = new DirectoryEntry(groupMember);
    lstUsers.Items.Add(member.Name);
}

you can download the source code here:
http://www.tabatabaei.info/csharpsamples/WindowsGroupMember.rar

11 comments:

pfoley33 said...

Dude!
That code was just the thing I was looking for - and simple too.

Thanks a million.
Paul.

Anonymous said...

Hi,
thanks verymuch for code to get the windows group user list,
How can I know the the particular user FirstName and LastName.
please let me know.

Thanks in advance,
Udaya Bhaskar.

Anonymous said...

thanks

Anonymous said...

This code is great!

One question, is there something that can be added to list out the nested group members?

冠毅 said...

Thank you very much

AL said...

This is great ! thanx a lot .

Anonymous said...

Thanks a lot...

Dilip said...

Thanks It is simple and Sweet

deepthiKiran said...

Hi ,
i have a problem in windows .i have one remote server i am able connect that one using this .But after deploying that code in server i am unable to.please help me

Thanks,
Deepthi Immadi

deepthiKiran said...

Hi ,
i have a problem in windows .i have one remote server i am able connect that one using this .But after deploying that code in server i am unable to.please help me

Thanks,
Deepthi Immadi

Anonymous said...

thankss