枚举子对象

要枚举容器中的对象,请创建可用于保存每一对象及其属性的 DirectoryEntry 对象。然后,可以使用 DirectoryEntry 对象提供的属性获取数据(例如 name 特性)。可以使用 Using foreach with Arrays 语句循环访问容器中的每个对象。

下面的代码示例说明如何枚举用户容器。

Try
    ' Bind to the container to enumerate.
    Dim ent As New DirectoryEntry("LDAP://CN=users,OU=Marketing,DC=fabrikam,DC=com")
    ' Create an object to use for individual objects in the container and iterate
    ' through the container.
    Dim child As DirectoryEntry
    For Each child In  ent.Children
        ' Write the name and path for each object in the container.
        Console.WriteLine("{0} {1}", child.Name, child.Path)
    Next child
Catch Exception1 As Exception
    ' Handle errors.
End Try
try
{
    // Bind to the container to enumerate.
    DirectoryEntry ent = new DirectoryEntry("LDAP://CN=users,OU=Marketing,DC=fabrikam,DC=com");
    // Create an object to use for individual objects in the container and iterate
    // through the container.
    foreach( DirectoryEntry child in ent.Children)
    {
        // Write the name and path for each object in the container.
        Console.WriteLine( "{0} {1}", child.Name, child.Path);
    }
}
catch
{
    // Handle errors.
}

另请参见

参考

System.DirectoryServices
DirectoryEntry

概念

导航目录

Send comments about this topic to Microsoft.

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。