string ADname = "LDAP://" + DomainName;
string Loginname = "";
string Loginpwd = "";
DirectoryEntry objDE = new DirectoryEntry(ADname, Loginname, Loginpwd);
string strFilter = "(&(&(objectCategory=person)(objectClass=user))(samaccountname=" + Loginname + "))";
DirectorySearcher objSearcher = new DirectorySearcher(objDE, strFilter);
SearchResult src = objSearcher.FindOne();
if (src != null)
{
Console.WriteLine(src.Path);
foreach (DictionaryEntry itemA in src.Properties)
{
StringBuilder sb = new StringBuilder();
foreach (var itemB in itemA.Value as ResultPropertyValueCollection)
{
sb.Append(itemB);
sb.Append(" / ");
}
Console.WriteLine("{0} : {1}", itemA.Key, sb.ToString().TrimEnd(' ', '/'));
}
Console.WriteLine("-".PadRight(50, '-'));
}
本文来自:http://blog.csdn.net/heivy/article/details/53505916