Compartir a través de


Walkthrough: Viewing Local Directory Objects

The procedures in this topic show how to use DirectoryEntry to list the users, groups, and services that are on your local computer. DirectoryEntry uses Active Directory Domain Services technology to access this information. 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, in the left pane, choose Visual Basic .NET, Visual C#, or Visual J#, 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 that is registered on your computer. Each user, group, and service will have two child nodes, one for its Active Directory Domain Services path and one for its properties.

To configure a DirectoryEntry component

  1. In Solution Explorer, right-click your project and click Add Reference on 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 Domain Services.

    Note

    If you user the incorrect domain or computer name, the application will throw an exception at run time. If you 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 Form1_Load method by adding the necessary 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 that are 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 either "User", "Group", or "Service". Use a select or switch statement to add a node to the correct group, depending on the directory entry type that is indicated by the SchemaClassName property.

    You will write the AddPathAndProperties method in the next step.

    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"
                users.Nodes.Add(newNode)
             Case "Group"
                groups.Nodes.Add(newNode)
             Case "Service"
                services.Nodes.Add(newNode)
          End Select
          AddPathAndProperties(newNode, child)
       Next
    End Sub
    
    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" :
                users.Nodes.Add(newNode);   
                break;
             case "Group" :
                groups.Nodes.Add(newNode);   
                break;
             case "Service" :
                services.Nodes.Add(newNode);   
                break;
          }
          AddPathAndProperties(newNode, child);
       }
    }
    
  3. Add the following code to the Form1 class to create the AddPathAndProperties method. The node parameter represents the TreeNode object that is associated with that entry. The entry parameter represents one entry in the directory that will be either 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.

    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")
       node.Nodes.Add(propertyNode)
    
       Dim propertyName As String
       Dim oneNode As String
       For Each propertyName In entry.Properties.PropertyNames
          Try
             oneNode = propertyName & ": " & _
                CType(entry.Properties(propertyName)(0), String)
          Catch
             oneNode = propertyName & ": " & _
                "No text representation."
          End Try
          propertyNode.Nodes.Add(New TreeNode(oneNode))
       Next
    End Sub
    
    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));
       }
    }
    

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.

This walkthrough used the WinNT service provider for Active Directory Domain Services. 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 that allow you to examine and manipulate directories.

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

See Also

Reference

System.DirectoryServices
DirectoryEntry
DirectorySearcher

Concepts

Getting Started in System.DirectoryServices

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.