#region 把AD域中得所有用户添加到下拉列表中
public void DataDropDown()
{
DirectoryEntry entry = new DirectoryEntry("LDAP://HPI.COM.CN"); //连接AD域
System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
//定义查询Seacher
mySearcher.Filter = "(&(objectClass=user)(objectCategory=person))";
//过滤从User组中得到人名
mySearcher.PropertiesToLoad.Add("name");//添加中文用户名
mySearcher.PropertiesToLoad.Add("samaccountname");//添加登陆名
SearchResultCollection resultCol = mySearcher.FindAll();//从AD中全部查询到内存中
foreach (SearchResult result in resultCol)
{
ResultPropertyCollection props = result.Properties;
string name = "";
string accountname = "";
foreach (string propName in props.PropertyNames)
{
bool flag = false;
if (propName == "name")
{
name = props[propName][0].ToString();
}
if (propName == "samaccountname")
{
accountname = props[propName][0].ToString();
flag = true;
}
}
DropDownList1.Items.Add(new ListItem(name, accountname));//将数据集潜入到下拉列表中
}
}
#endregion