Freigeben über


DirectorySearcher-Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie Sie alle Benutzer in einer Domäne suchen können. Es enthält einige, grundlegend erforderliche Aufgaben, wie z. B. die Verwendung einer "C# try-catch"- oder "Visual Basic .NET try-catch-finally"-Anweisung, um Fehler abzufangen, und die Fehlerbehandlung für alle Fehler, die vom DirectorySearcher-Objekt ausgelöst werden können.

Try
   ' Bind to the users container.
    Dim entry As New 
          DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com")

    ' Create a DirectorySearcher object.
    Dim mySearcher As New DirectorySearcher(entry)

    ' Create a SearchResultCollection object to hold a collection
    ' of SearchResults returned by the FindAll method.
    Dim result As SearchResultCollection = mySearcher.FindAll()
   
    ' Create an object to hold a single result from the 
    ' result collection.
    Dim resEnt1 As SearchResult

    ' Get search results. For more information, 
    ' see Getting Search Results.
    ' 
    ' This sample uses Try...Catch to catch errors.
    ' Create an Exception object. For more information, 
    ' see System.Exception.

Catch Exception1 As System.Runtime.InteropServices.COMException
    Console.WriteLine(Exception1.Message)

Catch Exception2 As InvalidOperationException
    Console.WriteLine(Exception2.Message)

Catch Exception3 As NotSupportedException
    Console.WriteLine(Exception3.Message)

End Try   
try
{
    // Bind to the users container.
    DirectoryEntry entry = new DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com");
    // Create a DirectorySearcher object.
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    // Create a SearchResultCollection object to hold a collection of SearchResults
    // returned by the FindAll method.
    SearchResultCollection result = mySearcher.FindAll();
    // Get search results. For more information, see Getting Search Results.
    // ...
    // This sample uses Try...Catch to catch errors.
    // Create an Exception object. For more information, see System.Exception.
}
catch (System.Runtime.InteropServices.COMException)
{
    System.Runtime.InteropServices.COMException exception = new System.Runtime.InteropServices.COMException();
    Console.WriteLine(exception);
}
catch (InvalidOperationException)
{
    InvalidOperationException InvOpEx = new InvalidOperationException(); 
    Console.WriteLine(InvOpEx.Message);
}
catch (NotSupportedException)
{
    NotSupportedException NotSuppEx = new NotSupportedException();
    Console.WriteLine(NotSuppEx.Message);
}

Siehe auch

Referenz

System.DirectoryServices
DirectorySearcher

Konzepte

Durchsuchen von Verzeichnissen

Weitere Ressourcen

Directory Synchronization Search Technology Sample

Send comments about this topic to Microsoft.

Copyright © 2007 Microsoft Corporation. Alle Rechte vorbehalten.