Microsoft KB Archive/315716

= How to access ADSI objects in Visual C# =

Article ID: 315716

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q315716



IN THIS TASK
SUMMARY
 * Requirements
 * Create a Demonstration Application
 * Test the Application

REFERENCES



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
 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. In the form designer, add a Button control to your form. By default, the button is named button1. In Solution Explorer, expand the ADSICS project. Right-click References, and then click Add Reference. 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. In Solution Explorer, expand References. Verify that System.DirectoryServices appears in the list of references.  Right-click the form, and then click View Code. Add the following statement at the start of the file: using System.DirectoryServices; </li> Define a Click event handler method for button1.</li>  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 = &quot;WinNT://YOURDOMAIN/YOURCOMPUTER&quot;; 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.

</li>  Add the following code to your event handler to display some of these properties: MessageBox.Show(de.Properties[&quot;OperatingSystem&quot;].Value.ToString,&quot;Operating System&quot;);

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

MessageBox.Show(de.Properties[&quot;Processor&quot;].Value.ToString,&quot;Processor&quot;);

</li></ol>

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

<div class="references_section">