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#.
- 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
- Basic Visual C# and ADSI programming techniques
- Start Visual Studio , and then create a new Visual C# .NET or Visual C# 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. Click OK to add this component reference to your project.
Note In Visual Studio 2003, you have to click
Select before you click OK.
- 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:
- Define a Click event handler method for button1.
- In the Click event handler method, declare a DirectoryEntry object, and then initialize it with the domain name and computer name of your computer: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.
DirectoryEntry de = new DirectoryEntry();
de.Path = "WinNT://YOURDOMAIN/YOURCOMPUTER<?xm-insertion_mark_start author="v-bobbid" time="20080716T095253-0800"?>,Computer<?xm-insertion_mark_end?>";
- Add the following code to your event handler to display some of these properties:
MessageBox.Show(de.Properties["OperatingSystemVersion"].Value.ToString(),"Operating System Version");
- Build and run the application.
- 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.
- Close the application.
For more information about ADSI, visit the following Microsoft Web site:
ID do Artigo: 315716 - Última Revisão: 2 de dez de 2008 - Revisão: 1