SearchResult 類別

定義

SearchResult 類別會封裝 Active Directory 網域服務階層架構中的節點,它是透過 DirectorySearcher 在搜尋期間所傳回的。The SearchResult class encapsulates a node in the Active Directory Domain Services hierarchy that is returned during a search through DirectorySearcher.

public ref class SearchResult
public class SearchResult
type SearchResult = class
Public Class SearchResult
繼承
SearchResult

範例

下列範例會建立具有所DirectoryEntry需路徑的新物件, 並FindOne使用方法來起始搜尋。The following example creates a new DirectoryEntry object with the desired path and uses the FindOne method to initiate the search. 執行搜尋之後, 此範例會使用GetDirectoryEntry方法來抓取搜尋結果中所識別的即時目錄專案。After performing the search, the example uses the GetDirectoryEntry method to retrieve the live directory entry identified in the search results.

Imports System  
Imports System.DirectoryServices  
Imports Microsoft.VisualBasic  
  
Public Class MySample  
   Public Shared Sub Main()  
      Dim myLDAPPath As String = ""  
      Try  
         ' Create a 'DirectoryEntry' object to search.  
         Console.WriteLine("Enter the path ( Ex : 'LDAP://MyServer')")  
         myLDAPPath = Console.ReadLine()  
         Dim mySearchRoot As New DirectoryEntry(myLDAPPath)  
  
         Dim myDirectorySearcher As New DirectorySearcher(mySearchRoot)  
  
         ' Get the first entry of the search.  
         Dim mySearchResult As SearchResult = myDirectorySearcher.FindOne()  
         If Not (mySearchResult Is Nothing) Then  
            ' Get the 'DirectoryEntry' that corresponds to 'mySearchResult'.  
            Dim myDirectoryEntry As DirectoryEntry = mySearchResult.GetDirectoryEntry()  
            Console.WriteLine(ControlChars.Newline + "The name of the 'myDirectoryEntry' " + _  
                        "directory entry that corresponds to the " + _  
                        "'mySearchResult' search result is : {0}" + _  
                        ControlChars.Newline, myDirectoryEntry.Name)  
            Dim mySearchResultPath As String = mySearchResult.Path  
            Console.WriteLine("The path for the 'mySearchResult' search result is : {0}" + _  
                              ControlChars.Newline, mySearchResultPath)  
            ' Get the properties of the 'mySearchResult'.  
            Dim myResultPropColl As ResultPropertyCollection  
            myResultPropColl = mySearchResult.Properties  
            Console.WriteLine("The properties of the 'mySearchResult' are :")  
            Dim myKey As String  
            For Each myKey In  myResultPropColl.PropertyNames  
               Dim tab1 As String = "    "  
               Console.WriteLine(myKey + " = ")  
               Dim myCollection As Object  
               For Each myCollection In  myResultPropColl(myKey)  
                  Console.WriteLine(tab1 + myCollection)  
               Next myCollection  
            Next myKey  
            myDirectoryEntry.Dispose()  
            mySearchRoot.Dispose()  
         Else  
            Console.WriteLine("The '" + myLDAPPath + "' path not found.")  
         End If  
      Catch e As Exception  
         Console.WriteLine("The '" + myLDAPPath + "' path not found.")  
         Console.WriteLine("Exception : " & e.Message)  
      End Try  
   End Sub 'Main  
End Class 'MySample  
using System;  
using System.DirectoryServices;  
  
public class MySample  
{  
    public static void Main()  
    {  
        string myLDAPPath = "";  
        try  
        {  
            // Create a 'DirectoryEntry' object to search.  
            Console.WriteLine("Enter the path ( Ex : 'LDAP://MyServer')");  
            myLDAPPath = Console.ReadLine();  
  
            DirectoryEntry mySearchRoot = new DirectoryEntry(myLDAPPath);  
            DirectorySearcher myDirectorySearcher =   
                    new DirectorySearcher(mySearchRoot);  
  
            // Get the first entry of the search.  
            SearchResult mySearchResult = myDirectorySearcher.FindOne();  
  
            if ( mySearchResult != null )  
            {  
                // Get the 'DirectoryEntry' that corresponds to 'mySearchResult'.  
                DirectoryEntry myDirectoryEntry =   
                mySearchResult.GetDirectoryEntry();  
                Console.WriteLine("\nThe name of the 'myDirectoryEntry' " +  
                        "directory entry that corresponds to the " +  
                        "'mySearchResult' search result is : {0}\n",  
                        myDirectoryEntry.Name);  
                string mySearchResultPath = mySearchResult.Path;  
                Console.WriteLine("The path for the 'mySearchResult' search "  
                        + "result is : {0}\n", mySearchResultPath);  
  
                // Get the properties of the 'mySearchResult'.  
                ResultPropertyCollection myResultPropColl;  
                myResultPropColl = mySearchResult.Properties;  
                Console.WriteLine("The properties of the " +   
                        "'mySearchResult' are :");  
  
                foreach( string myKey in myResultPropColl.PropertyNames)  
                {  
                    string tab = "    ";  
                    Console.WriteLine(myKey + " = ");  
                    foreach( Object myCollection in myResultPropColl[myKey])  
                    {  
                        Console.WriteLine(tab + myCollection);  
                    }  
                }  
                mySearchRoot.Dispose();  
                myDirectoryEntry.Dispose();  
            }  
            else  
            {  
                Console.WriteLine("The '" + myLDAPPath + "' path not found.");  
            }  
        }  
        catch(Exception e)  
        {  
            Console.WriteLine("The '" + myLDAPPath + "' path not found.");  
            Console.WriteLine("Exception : " + e.Message);  
        }  
    }  
  
}  
#using <mscorlib.dll>  
#using <System.dll>  
#using <System.Directoryservices.dll>  
  
using namespace System;  
using namespace System::Collections;  
using namespace System::DirectoryServices;  
using namespace stdcli::language;  
  
int main()   
{  
    String^ myLDAPPath = "";  
    try   
    {  
        // Create a 'DirectoryEntry' object to search.  
        Console::WriteLine("Enter the path ( Ex : 'LDAP://MyServer')");  
        myLDAPPath = Console::ReadLine();  
        DirectoryEntry^ mySearchRoot = gcnew DirectoryEntry(myLDAPPath);  
  
        DirectorySearcher^ myDirectorySearcher = gcnew DirectorySearcher(mySearchRoot);  
  
        // Get the first entry of the search.  
        SearchResult^ mySearchResult = myDirectorySearcher->FindOne();  
        if (mySearchResult)   
        {  
            // Get the 'DirectoryEntry' that corresponds to 'mySearchResult'.  
            DirectoryEntry^ myDirectoryEntry = mySearchResult->GetDirectoryEntry();  
            Console::WriteLine(  
                String::Concat("\nThe name of the 'myDirectoryEntry' ",  
                "directory entry that corresponds to the ",  
                "'mySearchResult' search result is : {0}\n"),  
                myDirectoryEntry->Name);  
  
            String^ mySearchResultPath = mySearchResult->Path;  
            Console::WriteLine("The path for the 'mySearchResult' search result is :  
                    {0}\n", mySearchResultPath);  
  
            // Get the properties of the 'mySearchResult'.  
            ResultPropertyCollection^ myResultPropColl = mySearchResult->Properties;  
            Console::WriteLine("The properties of the 'mySearchResult' are :");  
            IEnumerator^ myEnum = myResultPropColl->PropertyNames->GetEnumerator();  
            while (myEnum->MoveNext())   
            {  
                String^ myKey = safe_cast<String^>(myEnum->Current);  
                Console::WriteLine("{0} = ", myKey);  
                IEnumerator^ myEnum = myResultPropColl->Item[myKey]->GetEnumerator();  
                while (myEnum->MoveNext())   
                {  
                    Console::WriteLine("\t{0}", myEnum->Current);  
                }  
            }  
            myDirectoryEntry->Dispose();  
            mySearchRoot->Dispose();   
        }   
        else   
        {  
            Console::WriteLine("The '{0}' path not found.", myLDAPPath);  
        }  
    }   
    catch (Exception^ e)   
    {  
        Console::WriteLine("The '{0}' path not found.",  myLDAPPath);  
        Console::WriteLine("Exception : {0}", e->Message);  
    }  
}  
  

備註

類別的實例與類別的DirectoryEntry實例非常類似。 SearchResultInstances of the SearchResult class are very similar to instances of DirectoryEntry class. 重要的差異在於DirectoryEntry SearchResultCollection, 每次存取新的物件時, 類別會從 Active Directory Domain Services 階層中抓取其資訊, 而中的SearchResult資料則已在中取得從使用DirectorySearcher類別執行的查詢傳回。The crucial difference is that the DirectoryEntry class retrieves its information from the Active Directory Domain Services hierarchy each time a new object is accessed, whereas the data for SearchResult is already available in the SearchResultCollection, where it gets returned from a query that is performed with the DirectorySearcher class. 只有透過查詢中的DirectorySearcher.PropertiesToLoad集合所指定的屬性, 才可從SearchResult取得。Only those properties that are specified through the DirectorySearcher.PropertiesToLoad collection in your query will be available from SearchResult.

屬性

Path

取得這個 SearchResult 的路徑。Gets the path for this SearchResult.

Properties

取得這個物件之屬性的 ResultPropertyCollection 集合。Gets a ResultPropertyCollection collection of properties for this object.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetDirectoryEntry()

從 Active Directory 網域服務階層架構中擷取對應至 DirectoryEntrySearchResultRetrieves the DirectoryEntry that corresponds to the SearchResult from the Active Directory Domain Services hierarchy.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

安全性

DirectoryServicesPermission
LinkDemand

適用於