How to access ADSI objects in Visual C#


Active Directory Services Interface (ADSI) is a COM-based directory service model that you can use to access a wide variety of directory protocols, such as Windows Directory Service and Lightweight Directory Access Protocol (LDAP).

The documentation for ADSI contains many examples of instantiating components by using the GetObject function. The GetObject function is built into the Microsoft Visual Basic 6.0 programming language, but it is not part of the Visual C# environment. This article describes how to access ADSI objects in Visual C#.


The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Windows 98, Microsoft Windows Millennium Edition (Me), Microsoft Windows NT, Microsoft Windows 2000, Microsoft Windows Server 2003, or Microsoft Windows XP
  • Visual Studio .NET, including Visual C# .NET, Visual Studio 2005 including Visual C# 2005, and Visual Studio 2008 including Visual C# 2008
This article assumes that you are familiar with the following topics:
  • Basic Visual C# and ADSI programming techniques

Create a Demonstration Application

  1. Start Visual Studio , and then create a new Visual C# .NET or Visual C# Windows Application project named ADSICS.
  2. In the form designer, add a Button control to your form. By default, the button is named button1.
  3. In Solution Explorer, expand the ADSICS project.
  4. Right-click References, and then click Add Reference.
  5. In the Add Reference dialog box, click to select the System.DirectoryServices.dll component. Click OK to add this component reference to your project.

    Note In Visual Studio 2003, you have to click
    Select before you click OK.
  6. In Solution Explorer, expand References. Verify that System.DirectoryServices appears in the list of references.
  7. Right-click the form, and then click View Code. Add the following statement at the start of the file:
    using System.DirectoryServices;
  8. Define a Click event handler method for button1.
  9. In the Click event handler method, declare a DirectoryEntry object, and then initialize it with the domain name and computer name of your computer:
    DirectoryEntry de = new DirectoryEntry();

    de.Path = "WinNT://YOURDOMAIN/YOURCOMPUTER<?xm-insertion_mark_start author="v-bobbid" time="20080716T095253-0800"?>,Computer<?xm-insertion_mark_end?>";
    The DirectoryEntry object that you are binding to in this example is a computer object. A computer object exposes several properties, such as the operating system, the operating system version, and the processor.

  10. Add the following code to your event handler to display some of these properties:
    MessageBox.Show(de.Properties["OperatingSystem"].Value.ToString(),"Operating System");

    MessageBox.Show(de.Properties["OperatingSystemVersion"].Value.ToString(),"Operating System Version");


Test the Application

  1. Build and run the application.
  2. Click button1. Three message boxes appear in succession. The message boxes display the name of the operating system, the version of the operating system, and the processor name for the computer whose ADSI path is specified in the program.
  3. Close the application.


ADSI client libraries are included as part of Windows 2000 and later. Windows NT does not include ADSI client libraries. To download ADSI for Windows NT, visit the following Microsoft Web site: NOTE: This site also hosts the ADSI development SDK.

For more information about ADSI, visit the following Microsoft Web site:
Active Directory Service Interfaces Overview
For more information about the client extensions for Windows 98 and Windows NT, visit the following Microsoft Web site:
Active Directory Client Extensions for Windows 95, Windows 98 and Windows NT Workstation 4.0

ID articol: 315716 - Ultima examinare: 2 dec. 2008 - Revizie: 1