Export (0) Print
Expand All

SearchResult Class

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

System.Object
  System.DirectoryServices.SearchResult

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

[DirectoryServicesPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
public class SearchResult

The SearchResult type exposes the following members.

  NameDescription
Public propertyPathGets the path for this SearchResult.
Public propertyPropertiesGets a ResultPropertyCollection collection of properties for this object.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetDirectoryEntryRetrieves the DirectoryEntry that corresponds to the SearchResult from the Active Directory Domain Services hierarchy.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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

}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft