下面是一個例子功能查詢全局編錄:
class Program
{
static void Main()
{
DirectoryEntry entry = new DirectoryEntry("GC://dcserver.domain.local",
"utility",
"somepassword",
AuthenticationTypes.Secure);
const string searchString = "(&(objectCategory=person)(objectClass=user))";
DirectorySearcher searcher = new DirectorySearcher(entry,
searchString,
new string[] { "sAMAccountName", "cn" });
SearchResultCollection resultCollection = searcher.FindAll();
foreach (SearchResult result in resultCollection)
{
Console.WriteLine(result.Path + "\n" +
result.Properties["cn"][0] + "\n" +
result.Properties["samaccountname"][0] );
}
Console.ReadLine();
}
}