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.

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

Dim child As DirectoryEntry = Nothing
Dim entry As New _
    DirectoryEntry("LDAP://Fabrikam/CN=Users,DC=Fabrikam,DC=com")
Try
    child = entry.Children.Find("OU=Sales")
Catch
    ' Place error code here
End Try
If (child Is Nothing) Then
    Console.WriteLine("Sorry, child not found!")
Else
    Console.WriteLine(child.Name)
End If
DirectoryEntry child = null;
DirectoryEntry entry = new 
         DirectoryEntry("LDAP://Fabrikam/CN=Users,DC=Fabrikam,DC=com");
try
{
    child = entry.Children.Find("OU=Sales");
}
catch
{
    // Place error code here 
}
if (child == null)
    Console.WriteLine("Sorry, child not found!");
Else
Console.WriteLine(child.Name);

See Also

Reference

System.DirectoryServices
DirectoryEntry

Concepts

Navigating the Directory

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.