取得搜尋結果
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 方法。此方法傳回 SearchResult 物件的集合,這些物件包含在 SearchResultCollection 物件中。使用 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.
Copyright © 2007 by Microsoft Corporation.All rights reserved.