SearchResult 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
SearchResult 类可封装在通过 DirectorySearcher 进行搜索期间返回的、Active Directory 域服务层次结构中的节点。
public ref class SearchResult
public class SearchResult
type SearchResult = class
Public Class SearchResult
- 继承
-
SearchResult
示例
以下示例使用所需路径创建一个新的 DirectoryEntry 对象, FindOne 并使用 方法启动搜索。 执行搜索后,该示例使用 GetDirectoryEntry 方法检索搜索结果中标识的实时目录条目。
Imports System
Imports System.DirectoryServices
Imports Microsoft.VisualBasic
Public Class MySample
Public Shared Sub Main()
Dim myLDAPPath As String = ""
Try
' Create a 'DirectoryEntry' object to search.
Console.WriteLine("Enter the path ( Ex : 'LDAP://MyServer')")
myLDAPPath = Console.ReadLine()
Dim mySearchRoot As New DirectoryEntry(myLDAPPath)
Dim myDirectorySearcher As New DirectorySearcher(mySearchRoot)
' Get the first entry of the search.
Dim mySearchResult As SearchResult = myDirectorySearcher.FindOne()
If Not (mySearchResult Is Nothing) Then
' Get the 'DirectoryEntry' that corresponds to 'mySearchResult'.
Dim myDirectoryEntry As DirectoryEntry = mySearchResult.GetDirectoryEntry()
Console.WriteLine(ControlChars.Newline + "The name of the 'myDirectoryEntry' " + _
"directory entry that corresponds to the " + _
"'mySearchResult' search result is : {0}" + _
ControlChars.Newline, myDirectoryEntry.Name)
Dim mySearchResultPath As String = mySearchResult.Path
Console.WriteLine("The path for the 'mySearchResult' search result is : {0}" + _
ControlChars.Newline, mySearchResultPath)
' Get the properties of the 'mySearchResult'.
Dim myResultPropColl As ResultPropertyCollection
myResultPropColl = mySearchResult.Properties
Console.WriteLine("The properties of the 'mySearchResult' are :")
Dim myKey As String
For Each myKey In myResultPropColl.PropertyNames
Dim tab1 As String = " "
Console.WriteLine(myKey + " = ")
Dim myCollection As Object
For Each myCollection In myResultPropColl(myKey)
Console.WriteLine(tab1 + myCollection)
Next myCollection
Next myKey
myDirectoryEntry.Dispose()
mySearchRoot.Dispose()
Else
Console.WriteLine("The '" + myLDAPPath + "' path not found.")
End If
Catch e As Exception
Console.WriteLine("The '" + myLDAPPath + "' path not found.")
Console.WriteLine("Exception : " & e.Message)
End Try
End Sub 'Main
End Class 'MySample
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);
}
}
}
#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 = "";
try
{
// 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();
Console::WriteLine(
String::Concat("\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 = 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);
}
}
myDirectoryEntry->Dispose();
mySearchRoot->Dispose();
}
else
{
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);
}
}
注解
类的 SearchResult 实例与 类的 DirectoryEntry 实例非常相似。 关键区别在于,每次访问新对象时, DirectoryEntry 类都会从 Active Directory 域服务 层次结构中检索其信息,而 的数据SearchResult已在 中SearchResultCollection可用,从使用 类执行的DirectorySearcher查询中返回。 只有通过查询中的 DirectorySearcher.PropertiesToLoad 集合指定的那些属性才能从 SearchResult中使用。
属性
Path |
获取此 SearchResult 的路径。 |
Properties |
获取此对象的属性的 ResultPropertyCollection 集合。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetDirectoryEntry() |
从 Active Directory 域服务层次结构检索与 SearchResult 相对应的 DirectoryEntry。 |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |