列舉子物件

若要列舉容器中的物件,請建立可以用來保留每個物件和其屬性的 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.

Copyright © 2007 by Microsoft Corporation.All rights reserved.