Directory Services

A Simple System.DirectoryServices Application

This is a small application that produces a fully functional System.DirectoryServices console. This application searches Active Directory based on an ambiguous name, which could be a user name, user's first name, user's last name, telephone Number, or office location, and displays the result. This application is created in Visual Studio .NET.

Creating a System.DirectoryServices Console Application

  1. Open Visual Studio .NET and select New Project.
  2. From Project Types, select Visual C# or Visual Basic and from Templates, select Console Application.
  3. Name your project and select OK.
  4. Select Project>Add Reference... and select System.DirectoryServices from the list displayed on the .NET tab.
  5. Add the "using System.DirectoryServices;" statement to the end of the using statement list.
  6. Add the following lines to the Class1 Main module.
    [C#]
    using System.DirectoryServices;
    ... 
    DirectorySearcher src = new DirectorySearcher("(anr=putANameHere)");
    foreach(SearchResult res in src.FindAll() )
    {
    	Console.WriteLine("{0} {1}", res.Properties["cn"][0], res.Properties["telephoneNumber"][0]);
    }
    
    [Visual Basic .NET]
    Imports System.DirectoryServices
    ....
    Dim src As DirectorySearcher = New DirectorySearcher("(anr=putANameHere)") 
    Dim result As SearchResult
    For Each result In src.FindAll()
    	Console.WriteLine("{0} {1}", result.Properties("Name")(0), result.Properties("telephoneNumber")(0))
    Next
    
  7. Compile and run your application.

For more information and a code example of a System.DirectoryServices application that uses a Windows form, see Enumerating User Memberships.