下面的代码允许我从 DirectoryServices 中提取整个全局地址列表。代码的功能在于它给了我我需要的东西。问题是返回 1000 个对象大约需要 20 秒。我能做些什么来加快速度吗?
public static List<Address> GetGlobalAddressList()
{
using (var searcher = new DirectorySearcher())
{
using (var entry = new DirectoryEntry(searcher.SearchRoot.Path, "*****", "*****"))
{
searcher.Filter = "(&(mailnickname=*)(objectClass=user))";
searcher.PropertiesToLoad.Add("cn");
searcher.PropertyNamesOnly = true;
searcher.SearchScope = SearchScope.Subtree;
searcher.Sort.Direction = SortDirection.Ascending;
searcher.Sort.PropertyName = "cn";
var results = searcher.FindAll();
var addressList = new List<Address>();
foreach (SearchResult i in results)
{
var address = new Address
{
DisplayName = (string)i.GetDirectoryEntry().Properties["displayName"].Value,
Mail = (string) i.GetDirectoryEntry().Properties["mail"].Value
};
addressList.Add(address);
}
return addressList;
}
}
}
public class Address
{
public string DisplayName { get; set; }
public string Mail { get; set; }
}