阳光地带

风雨雷电

博客园 首页 新随笔 联系 订阅 管理

#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

posted on 2007-05-10 16:17  阳光地带  阅读(524)  评论(0编辑  收藏  举报