Navigating to the Child Object

Every DirectoryEntry object in a directory has a property called Children that is a collection object used to navigate to a child object for that directory entry. To get to a specific child in the collection, you can use the Find method.

Children obtains data about related objects from the DirectoryEntries collection. For example, you could navigate to the users object on a domain (LDAP://fabrikam/cn=users,dc=fabrikam,dc=com) and use Children to view all the users on that domain. Each user listed in the Children collection is an entry in the directory, so you can view that DirectoryEntries is a collection of DirectoryEntry objects that are children of a higher level directory object.

The following code example shows how to enumerate a list of objects in a Children collection.

[Visual Basic .NET]

Dim ent As New DirectoryEntry("LDAP://Fabrikam/CN=Users,DC=Fabrikam,DC=com")
Dim child As DirectoryEntry
For Each child In ent.Children
    Console.WriteLine(child.Name)
Next child

[C#]

DirectoryEntry ent = new DirectoryEntry("LDAP://Fabrikam/CN=Users,DC=Fabrikam,DC=com");
foreach (DirectoryEntry child in ent.Children)
     Console.WriteLine(child.Name);

The following code example shows how to navigate to a specific child in the Children collection, using the Find method.

[Visual Basic .NET]

Dim child As DirectoryEntry = ent.Children.Find("OU=Sales")
      
 If (True) Then
     Console.WriteLine(child.Name)
 End If

[C#]

DirectoryEntry child = ent.Children.Find("OU=Sales");
     Console.WriteLine(child.Name);