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");
                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));
                    String treeName = child.Name.ToString().Remove(0, 3);
                    nodes.Nodes.Add(new TreeNode(treeName));
            return nodes;

Open in new window

Who is Participating?
oBdAConnect With a Mentor Commented:
Probably; I'm not a programmer, but you might have a look at these for starters, as they might keep you from reinventing the wheel:

Directory Object Picker

Using the Windows 2000/XP Object Selection Dialog
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.