This documentation is archived and is not being maintained.

Walkthrough: Adding Active Directory Objects

Visual Studio .NET 2003

The procedures in this topic demonstrate the process of using the DirectoryEntry component to list the users, groups, and services on your local computer. The DirectoryEntry component uses Active Directory technology to do this work. Each entry created by the component contains a list of its properties.

To create the user interface

  1. On the File menu, point to New, and then click Project.
  2. In the New Project dialog box, choose Visual Basic or Visual C# in the left pane, then choose the Windows Application template. Name the project ActiveDirectory.
  3. From the Windows Forms tab of the Toolbox, drag a TreeView control onto Form1.
  4. Set the Name property of the TreeView control to viewPC.

    The TreeView control will be modified later in the walkthrough to contain three top-level nodes, one each for users, groups, and services. Each second-level node will represent one user, group, or service registered on your computer. Each user, group, and service will have two child nodes, one for its Active Directory path and one for its properties.

To configure a DirectoryEntry component

  1. In Solution Explorer, right-click your project and select Add Reference from the shortcut menu.
  2. Add a reference to System.DirectoryServices.dll.
  3. From the Components tab of the Toolbox, drag a DirectoryEntry component instance to the designer.
  4. Set the Name property of the DirectoryEntry component to entryPC.
  5. Set the Path property of the DirectoryEntry component to WinNT://Domain/YourComputerName. Use your domain and computer name to direct the DirectoryEntry component to examine your local computer using the WinNT service provider for Active Directory.
    Note   If you do not know your domain or computer name, contact your system administrator.
    Tip   If you have the incorrect domain or computer name, the application will throw an exception at run time. You can enter WinNT://YourComputerName for the path, and the service provider will attempt to locate the computer.

To add the top-level nodes to the TreeView control

  1. In the designer, double-click Form1 to create the Form_Load event handler in the Code Editor.
  2. Modify the Form_Load method by adding code to create the three top-level nodes for users, groups, and services. When the application runs, the Children property of the component named entryPC contains all the entries found in Path (your computer). Each child is also a DirectoryEntry object. Because you are using the WinNT provider on a computer path, the SchemaClassName property of the child DirectoryEntry object will be one of "User", "Group", or "Service". A select or switch statement can be used to determine the entry type and add a node to the correct group.

    You will write the AddPathAndProperties method in the next step.

    ' Visual Basic
    Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
       Dim users As New TreeNode("Users")
       Dim groups As New TreeNode("Groups")
       Dim services As New TreeNode("Services")
       viewPC.Nodes.AddRange(New TreeNode() {users, groups, services})
       Dim child As System.DirectoryServices.DirectoryEntry
       For Each child In entryPC.Children
          Dim newNode As New TreeNode(child.Name)
          Select Case child.SchemaClassName
             Case "User"
             Case "Group"
             Case "Service"
          End Select
          AddPathAndProperties(newNode, child)
    End Sub
    // C#
    private void Form1_Load(object sender, System.EventArgs e)
       TreeNode users = new TreeNode("Users");
       TreeNode groups = new TreeNode("Groups");
       TreeNode services = new TreeNode("Services");
       viewPC.Nodes.AddRange(new TreeNode[] { users, groups, services });
       foreach(System.DirectoryServices.DirectoryEntry child 
          in entryPC.Children) 
          TreeNode newNode = new TreeNode(child.Name);
          switch (child.SchemaClassName) 
             case "User" :
             case "Group" :
             case "Service" :
          AddPathAndProperties(newNode, child);
  3. Add the following code to the Form1 class to create the AddPathAndProperties method. The node parameter represents the TreeNode object associated with that entry. The entry parameter represents one entry in the directory: a user, a group, or a service.

    The DirectoryEntry component has a Properties property that contains a string-indexed collection of properties. The contents of this collection depend on the schema of the DirectoryEntry component. The Properties collection has a PropertyNames property, which is a collection of all the names of the properties. By stepping through this collection, you can retrieve each property. Each property also has a collection of associated values. In this method, you only retrieve the first value for each property.

    ' Visual Basic
    Private Sub AddPathAndProperties(ByVal node As TreeNode, _
    ByVal entry As System.DirectoryServices.DirectoryEntry)
       node.Nodes.Add(New TreeNode("Path: " & entry.Path))
       Dim propertyNode As New TreeNode("Properties")
       Dim propertyName As String
       Dim oneNode As String
       For Each propertyName In entry.Properties.PropertyNames
             oneNode = propertyName & ": " & _
                CType(entry.Properties(propertyName)(0), String)
             oneNode = propertyName & ": " & _
                "No text representation."
          End Try
          propertyNode.Nodes.Add(New TreeNode(oneNode))
    End Sub
    // C#
    private void AddPathAndProperties(TreeNode node, 
       System.DirectoryServices.DirectoryEntry entry)
       node.Nodes.Add(new TreeNode("Path: " + entry.Path));
       TreeNode propertyNode = new TreeNode("Properties");
       foreach (string propertyName in entry.Properties.PropertyNames) 
          string oneNode = propertyName + ": " + 
          propertyNode.Nodes.Add(new TreeNode(oneNode));

To run the program

  1. Press F5 to run the program.
  2. Open the nodes to examine the paths and properties. The users all share the same set of properties, because they all share the same schema. The same is true of the group and service nodes.

Next Steps

This walkthrough used the WinNT service provider for Active Directory. Other services are available, including the Lightweight Directory Access Protocol (LDAP), the Novell NetWare Directory Service (NDS), and the Novell Netware 3.x service (NWCOMPAT). Each provider provides a different set of objects to allow you to examine and manipulate directories.

The DirectorySearcher component can be used to search for entries from a root path. The DirectorySearcher works with the LDAP provider.

See Also

Creating Active Directory Components | Active Directory Technology Backgrounder | DirectoryEntry Class | DirectorySearcher Class