对域中OU的遍历

protected void Page_Load(object sender, EventArgs e)
    {
        DirectoryEntry de = new DirectoryEntry(ldap, admin, pwd);
        TreeNode root = new TreeNode("root", "root");
        TreeNode tn;
        foreach (DirectoryEntry child in de.Children)  //循环遍历根目录下的OU
        {
            if (child.Name.Contains("OU"))
            {
                string ou = child.Name.Split(new char[] { '=' })[1].ToString();
                string url = child.Properties["distinguishedName"].Value.ToString();
               
                DirectoryEntry dir = new DirectoryEntry(ldapIP+url,admin, pwd);
                tn = new TreeNode();
                tn.Text = ou;

               
                this.treeView(dir, tn);
                root.ChildNodes.Add(tn);

            }
        }
        this.TreeView1.Nodes.Add(root);

    }

public void treeView(DirectoryEntry de,TreeNode root) //遍历每个根目录OU中的OU
    {
        foreach (DirectoryEntry child in de.Children)
        {
            if (child.Name.Contains("OU"))
            {
                string ou = child.Name.Split(new char[] { '=' })[1].ToString();
              
                string url = child.Properties["distinguishedName"].Value.ToString();
                DirectoryEntry dir = new DirectoryEntry(ldapIP+url, admin, pwd);
                TreeNode tree = new TreeNode();
                tree.Text = ou;
               
                this.treeView(dir, tree);
                root.ChildNodes.Add(tree);

     
            }
            else
            {
                TreeNode leaf = new TreeNode();
                if (child.Properties["displayName"].Value != null)
                {
                    leaf.Text = child.Properties["displayName"].Value.ToString();
                    if (child.Properties["mobile"].Value != null)
                    {
                        leaf.Value = child.Properties["mobile"].Value.ToString();
                    }
                    else
                    {
                        leaf.Value = "0";
                    }
                    root.ChildNodes.Add(leaf);
                }
            }
          
           
        }
    }

posted on 2007-04-27 11:40  大胖头  阅读(617)  评论(0编辑  收藏  举报