获取搜索结果

DirectorySearcher 返回的每个结果都可作为 SearchResult 对象检索。每个 SearchResult 对象均包含单个结果及其关联属性。

每个 SearchResult 返回的属性都包含在 ResultPropertyCollection 对象中。这些属性的值包含在 ResultPropertyValueCollection 对象中。

FindOne 方法将返回单个 SearchResult。下面的示例说明如何使用 FindOne 方法获取单个 SearchResult,并从 ResultPropertyValueCollection 检索其所有属性值。

' Bind to a specific user.
Dim path As String
path = "LDAP://CN=User Name,CN=users, DC=fabrikam,DC=com"
Dim entry As New DirectoryEntry(path)

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

' Use the FindOne method to find the user object.
Dim resEnt As SearchResult = mySearcher.FindOne()

Dim propKey As String
For Each propKey In resEnt.Properties.PropertyNames
    ' Display each of the values for the property 
    ' identified by the property name.
    Dim prop As Object
    For Each prop In resEnt.Properties(propKey)
        Console.WriteLine("{0}:{1}", propKey, [prop].ToString())
    Next prop
Next propKey
// Bind to a specific user.
DirectoryEntry entry = new DirectoryEntry(
"LDAP://CN=User Name,CN=users,DC=fabrikam,DC=com");

// Create a DirectorySearcher object.
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.SearchScope = SearchScope.Base;

// Use the FindOne method to find the user object.
SearchResult resEnt = mySearcher.FindOne();

foreach(string propKey in resEnt.Properties.PropertyNames)
{
    // Display each of the values for the property 
    // identified by the property name.
    foreach (object property in resEnt.Properties[propKey])
    {
        Console.WriteLine("{0}:{1}", propKey, property.ToString());
    }
}

在搜索检索一个或多个结果时,使用 FindAll 方法。此方法返回在 SearchResultCollection 对象中包含的 SearchResult 对象的集合。使用 foreach 语句循环访问 SearchResultCollection 并从单个 SearchResult 对象获取数据。

下面的示例使用通配符搜索筛选器和 FindAll 方法查找 Users 容器中用户名以“test”开头的所有用户。还从 ResultPropertyValueCollection 对象检索与结果集中每个对象关联的所有属性值。

Dim results As SearchResultCollection = Nothing

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

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

    ' Set a filter for users with the name test.
    mySearcher.Filter = "(&(objectClass=user)(anr=test*))"

    ' Use the FindAll method to return objects to a SearchResultCollection.
    results = mySearcher.FindAll()

    ' Iterate through each SearchResult in the SearchResultCollection.
    Dim searchResult As SearchResult
    For Each searchResult In results
        ' Display the path of the object found.
        Console.WriteLine("Search properties for {0}", _
            searchResult.Path)

        ' Iterate through each property name in each SearchResult.
        Dim propertyKey As String
        For Each propertyKey In searchResult.Properties.PropertyNames
            ' Retrieve the value assigned to that property name 
            ' in the ResultPropertyValueCollection.
            Dim valueCollection As ResultPropertyValueCollection = searchResult.Properties(propertyKey)

            ' Iterate through values for each property name in each SearchResult.
            Dim propertyValue As Object
            For Each propertyValue In valueCollection
                ' Handle results. Be aware that the following 
                ' WriteLine() only returns readable results for 
                ' properties that are strings.
                Console.WriteLine("{0}:{1}", _
                    propertyKey, _
                    propertyValue.ToString())
            Next propertyValue
        Next propertyKey
    Next searchResult
Finally
    ' To prevent memory leaks, always call 
    ' SearchResultCollection.Dispose() manually.
    If Not results Is Nothing Then
        results.Dispose()
        results = Nothing
    End If
End Try
SearchResultCollection results = null;

try
{
    // Bind to the users container.
    string path = "LDAP://CN=users,DC=fabrikam,DC=com";
    DirectoryEntry entry = new DirectoryEntry(path);

    // Create a DirectorySearcher object.
    DirectorySearcher mySearcher = new DirectorySearcher(entry);

    // Set a filter for users with the name test.
    mySearcher.Filter = "(&(objectClass=user)(anr=test*))";

    // Use the FindAll method to return objects to a 
    // SearchResultCollection.
    results = mySearcher.FindAll();

    // Iterate through each SearchResult in the SearchResultCollection.
    foreach (SearchResult searchResult in results)
    {
        // Display the path of the object found.
        Console.WriteLine("Search properties for {0}", searchResult.Path);

        // Iterate through each property name in each SearchResult.
        foreach (string propertyKey in 
            searchResult.Properties.PropertyNames)
        {
            // Retrieve the value assigned to that property name 
            // in the ResultPropertyValueCollection.
            ResultPropertyValueCollection valueCollection = 
                searchResult.Properties[propertyKey];

            // Iterate through values for each property name in each 
            // SearchResult.
            foreach (Object propertyValue in valueCollection)
            {
                // Handle results. Be aware that the following 
                // WriteLine only returns readable results for 
                // properties that are strings.
                Console.WriteLine(
                    "{0}:{1}", 
                    propertyKey, 
                    propertyValue.ToString());
            }
        }
    }
}
finally
{
    // To prevent memory leaks, always call 
    // SearchResultCollection.Dispose() manually.
    if (null != results)
    {
        results.Dispose();
        results = null;
    }
}

若要仅检索在前面代码示例的结果集中返回的对象的特定属性值,可以将内部的两条 foreach 语句替换为提供所需属性名称的语句,如下面的语句所示。

Console.WriteLine(resEnt1.Properties("cn")(0))
Console.WriteLine(resEnt1.Properties("objectClass")(1))
Console.WriteLine(resEnt1.Properties["cn"][0]);
Console.WriteLine(resEnt1.Properties["objectClass"][1]);

在这些语句中,为特定属性命名并建立索引。如果这些属性包含多个值,则需要枚举这些值。有关枚举属性值的详细信息,请参阅读取包含多个值的属性

ResultPropertyValueCollection 对象提供的另一个选项是 Item 属性,该属性检索包含多个值的属性中指定索引位置处的值。Item 关键字在 Visual Basic .NET 中使用,但在 C# 中,Item 的实现是包含要检索值的索引位置的数组。下面的示例说明如何使用 Item

' Bind to a specific user.
Dim path As String = "LDAP://CN=User Name,CN=users,DC=fabrikam,DC=com"
Dim entry As New DirectoryEntry(path)

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

' Use the FindOne method to find the object, which in this case, is the user
' indicated by User Name and assign it to a SearchResult.
Dim searchResult As SearchResult = searcher.FindOne()

' Create a ResultPropertyValueCollection object to get the values for the 
' memberOf attribute for this user.
Dim propertyName As String = "memberOf"
Dim valueCollection As ResultPropertyValueCollection = searchResult.Properties(propertyName)

Try
    ' Write the value contained in index position 5 in the memberOf attribute.
    Console.WriteLine(valueCollection(5).ToString())
Catch argumentEx As ArgumentOutOfRangeException
    ' The property contains no value in position 5.
    Console.WriteLine("The {0} property contains no value at the specified index.", propertyName)
End Try
// Bind to a specific user.
string path = "LDAP://CN=User Name,CN=users,DC=fabrikam,DC=com";
DirectoryEntry entry = new DirectoryEntry(path);

// Create a DirectorySearcher object.
DirectorySearcher searcher = new DirectorySearcher(entry);

// Use the FindOne method to find the object, which in this case, is the user
// indicated by User Name, and assign it to a SearchResult.
SearchResult searchResult = searcher.FindOne();

// Create a ResultPropertyValueCollection object to get the values for the 
// memberOf attribute for this user.
string propertyName = "memberOf";
ResultPropertyValueCollection valueCollection = searchResult.Properties[propertyName];

try
{
    // Write the value contained in index position 5 in the memberOf attribute.
    Console.WriteLine(valueCollection[5].ToString());
}
catch (ArgumentOutOfRangeException)
{
    // The property contains no value in position 5.
    Console.WriteLine(
        "The {0} property contains no value at the specified index.", 
        propertyName);
}

对于搜索结果,System.DirectoryServices 不支持以下返回类型:

  • ADS_PROV_SPECIFIC
  • ADSTYPE_CASEIGNORE_LIST
  • ADSTYPE_OCTET_LIST
  • ADSTYPE_PATH
  • ADSTYPE_POSTALADDRESS
  • ADSTYPE_TIMESTAMP
  • ADSTYPE_NETADDRESS
  • ADSTYPE_FAXNUMBER
  • ADSTYPE_EMAIL
  • ADSTYPE_BACKLINK
  • ADSTYPE_HOLD
  • ADSTYPE_TYPEDNAME
  • ADSTYPE_REPLICAPOINTER
  • ADSTYPE_UNKNOWN
  • ADSTYPE_PROV_SPECIFIC

另请参见

参考

System.DirectoryServices
DirectorySearcher
SearchResult
SearchResultCollection
ResultPropertyCollection
ResultPropertyValueCollection

概念

搜索目录
读取包含多个值的属性

Send comments about this topic to Microsoft.

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。