検索結果の取得
DirectorySearcher によって返される各結果は、SearchResult オブジェクトとして取得できます。各 SearchResult オブジェクトには、1 つの結果とそれに関連付けられたプロパティが含まれています。
各 SearchResult で返されるプロパティは、ResultPropertyCollection オブジェクトに含まれています。これらのプロパティの値は、ResultPropertyValueCollection オブジェクトに含まれています。
FindOne メソッドは、1 つの SearchResult を返します。次の例は、FindOne メソッドを使用して 1 つの 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());
}
}
検索で 1 つ以上の結果を取得する場合は、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;
}
}
前のコード例の結果セットで返されるオブジェクトの特定のプロパティ値のみを取得するには、次のステートメントに示すように、中の 2 つの 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 オブジェクトで提供されるもう 1 つのオプションは、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.