private static void GetAllUsersInAD()
{
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot = new DirectoryEntry("LDAP://a14907.com", "username", "pwd", AuthenticationTypes.Encryption);
searcher.Filter = "(objectClass=USER)";
searcher.SearchScope = SearchScope.Subtree;
searcher.Sort = new SortOption("name", SortDirection.Ascending);
searcher.PageSize = int.MaxValue - 100;
searcher.PropertiesToLoad.AddRange(new string[] { "samaccountname", "company", "department", "mail" });
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult item in results)
{
//操作
}
}