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

12 comments:

Edward 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?

LeEkY 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

Sandeep N said...

Can you please share the link to download the code. I really need this code for me to fetch the data from 2000 servers.