Link to home
Start Free TrialLog in
Avatar of invisiblekhaos2
invisiblekhaos2

asked on

Mimic AD in a TreeView through C#

What I'm trying to do is query our Domain Controller for all of the Active Directory objects and mimic that view in a C# TreeView. The code I attached below ~works~ but it takes forever to load and hogs up system resources. Is there a simpler way to do this? I just need to make the TreeView look exactly like AD (Ou's, users' computer's, etc). Is there a better way to do this than below?


private static TreeNode RecursiveComputeLeafNodes(String ou, TreeNode nodes)
        {
            DirectoryEntry entry = new DirectoryEntry();
            if (ou.Equals(""))
                entry = new DirectoryEntry("LDAP://dc=corp,dc=pizzagalli,dc=com");
            else 
                entry = new DirectoryEntry(ou);
            //DirectorySearcher mySearcher = new DirectorySearcher(entry);
            //mySearcher.Filter = "(objectClass=computer)";
            foreach (System.DirectoryServices.DirectoryEntry child in entry.Children)
            {
                if (child.Properties.Count != 0)
                {
                    TreeNode newTreeNode = new TreeNode(child.Name.ToString().Remove(0,3));
                    nodes.Nodes.Add(RecursiveComputeLeafNodes(child.Path.ToString(), newTreeNode));
                }
                else
                {
                    String treeName = child.Name.ToString().Remove(0, 3);
                    nodes.Nodes.Add(new TreeNode(treeName));
                }
            }
            return nodes;
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial