Hey guys, I am writing an Active Directory Tool that allows my team to read data without having to access Active Directory through a remote desktop session. I am not too familiar with the TreeView control and I was wondering if there was a way to add the Active Directory hierarchy starting at the root and populating it to a TreeView control. Any suggestions would be greatly appreciated.
Thanks,
Rashawn

I took the bulk of this code from an MSDN example. I have used TreeView a few times, but not Active Directory objects. To use, create a form and drag a TreeView control and a DirectoryEntry component onto the form, and wire the Load event...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ForumSolutions
{
    public partial class Form_ActiveDirectoryTreeView : Form
    {
        public Form_ActiveDirectoryTreeView()
        {
            InitializeComponent();
        }
        private void Form_ActiveDirectoryTreeView_Load(object sender, System.EventArgs e)
        {
            TreeNode users = new TreeNode("Users");
            TreeNode groups = new TreeNode("Groups");
            TreeNode services = new TreeNode("Services");
            treeView1.Nodes.AddRange(new TreeNode[] { users, groups, services });

            directoryEntry1.Path = @"WinNT://"+ Environment.MachineName;//WinNT://Domain/YourComputerName

            foreach (System.DirectoryServices.DirectoryEntry child
               in directoryEntry1.Children)
            {
                TreeNode newNode = new TreeNode(child.Name);
                switch (child.SchemaClassName)
                {
                    case "User":
                        users.Nodes.Add(newNode);
                        break;
                    case "Group":
                        groups.Nodes.Add(newNode);
                        break;
                    case "Service":
                        services.Nodes.Add(newNode);
                        break;
                }
                AddPathAndProperties(newNode, child);
            }
        }

        private void AddPathAndProperties(TreeNode node,
           System.DirectoryServices.DirectoryEntry entry)
        {
            node.Nodes.Add(new TreeNode("Path: " + entry.Path));
            TreeNode propertyNode = new TreeNode("Properties");
            node.Nodes.Add(propertyNode);
            foreach (string propertyName in entry.Properties.PropertyNames)
            {
                string oneNode = propertyName + ": " +
                   entry.Properties[propertyName][0].ToString();
                propertyNode.Nodes.Add(new TreeNode(oneNode));
            }
        }
    }
}

Hey dDoubled, I tried putting this code snippet into my program and on the button click event I only see Users, Groups, and Services setup as parents nodes in the TreeView Control. I do not have the option of expanding the Node to see its children. Active Directory hierarchy does not populate the TreeView, any other ideas as to what I can try to get it working? See code snippet below.

private void button2_Click(object sender, EventArgs e)
        {
            TreeNode users = new TreeNode("Users");
            TreeNode groups = new TreeNode("Groups");
            TreeNode services = new TreeNode("Services");

            treeView1.Nodes.AddRange(new TreeNode[] { users, groups, services });

            DirectoryEntry de = new DirectoryEntry(LDAP_DOMAIN, LDAP_UID, LDAP_PWD, AuthenticationTypes.Secure);

            foreach (System.DirectoryServices.DirectoryEntry child in de.Children)
            {
                TreeNode newNode = new TreeNode(child.Name);

                switch (child.SchemaClassName)
                {
                    case "User":
                        users.Nodes.Add(newNode);
                        break;
                    case "Groups":
                        groups.Nodes.Add(newNode);
                        break;
                    case "Services":
                        services.Nodes.Add(newNode);
                        break;
                }
                AddPathAndProperties(newNode, child);
            }

        }
        private void AddPathAndProperties(TreeNode node, System.DirectoryServices.DirectoryEntry de)
        {
            node.Nodes.Add(new TreeNode("Path: " + de.Path));
            TreeNode propertyNode = new TreeNode("Properties");
            node.Nodes.Add(propertyNode);

            foreach (string propertyName in de.Properties.PropertyNames)
            {
                string oneNode = propertyName + ": " + de.Properties[propertyName][0].ToString();
                propertyNode.Nodes.Add(new TreeNode(oneNode));
            }


        }

I made a mistake, I got it working! Hey thanks a lot DdoubleD for your help!!!

This question has already been answered. Start a new discussion instead.