SearchResult Class


The SearchResult class encapsulates a node in the Active Directory Domain Services hierarchy that is returned during a search through DirectorySearcher.

Namespace:   System.DirectoryServices
Assembly:  System.DirectoryServices (in System.DirectoryServices.dll)


	Unrestricted = true)]
public ref class SearchResult 


Gets the path for this SearchResult.


Gets a ResultPropertyCollection collection of properties for this object.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Retrieves the DirectoryEntry that corresponds to the SearchResult from the Active Directory Domain Services hierarchy.


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

Instances of the SearchResult class are very similar to instances of DirectoryEntry class. 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. Only those properties that are specified through the DirectorySearcher::PropertiesToLoad collection in your query will be available from SearchResult.

The following example creates a new DirectoryEntry object with the desired path and uses the FindOne method to initiate the search. After performing the search, the example uses the GetDirectoryEntry method to retrieve the live directory entry identified in the search results.

#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 = "";
        // 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();
                String::Concat("\nThe name of the 'myDirectoryEntry' ",
                "directory entry that corresponds to the ",
                "'mySearchResult' search result is : {0}\n"),

            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);
            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);

.NET Framework
Available since 1.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top