The following code allows me to extract the entire Global Address List from DirectoryServices. The code is functional in that it gives me what I need. The problem is that it takes about 20 seconds to return 1000 objects. Is there anything that I can do to speed this up?
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; }
}