Directory Services

Enumerating Users in a Group

This topic provides code examples for finding all the members of a group. If the group has many members, you can get a result set by invoking IADsGroup::Members.

The following code example shows how to get members using the SearchResult property Properties.

[C#]
DirectoryEntry group = new DirectoryEntry("LDAP://CN=Sales,DC=Fabrikam,DC=COM");
foreach(object dn in group.Properties["member"] )
{
   Console.WriteLine(dn);
}

The following code example shows how to get members using the Invoke method to invoke the ADSI IADsGroup::Members method.

[C#]
DirectoryEntry group = new DirectoryEntry("LDAP://CN=Sales,DC=Fabrikam,DC=COM");
object members = group.Invoke("Members",null);
foreach( object member in (IEnumerable) members)
{
DirectoryEntry x = new DirectoryEntry(member);
Console.WriteLine(x.Name);
}