How to access ADSI objects in Visual C# (315716)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# 2005, Express Edition

This article was previously published under Q315716

SUMMARY

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#.

back to the top

Requirements

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 or Visual Studio 2005 including Visual C# 2005
This article assumes that you are familiar with the following topics:
  • Basic Visual C# and ADSI programming techniques
back to the top

Create a Demonstration Application

  1. Start Visual Studio .NET or Visual Studio 2005, and then create a new Visual C# .NET or Visual C# 2005 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, and then click Select. Click OK to add this component reference to your project.

    Note In Visual Studio 2005, you do not have to click Select.
  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";
    					
    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");
    
    MessageBox.Show(de.Properties["Processor"].Value.ToString(),"Processor");
    
    					
back to the top

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.
back to the top

REFERENCES

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: 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
http://www.microsoft.com/technet/archive/ntwrkstn/downloads/utils/dsclient.mspx

back to the top

Modification Type:MajorLast Reviewed:3/16/2006
Keywords:kbHOWTOmaster KB315716 kbAudDeveloper