Microsoft KB Archive/326091

= How to poll for changes to the Active Directory by using Visual C# =

Article ID: 326091

Article Last Modified on 5/23/2007

-

APPLIES TO


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

-



This article was previously published under Q326091



INTRODUCTION
This article describes how to poll for changes to the Active Directory by using Microsoft Visual C# .NET or Microsoft Visual C#. This article contains sample code that demonstrates how to do this.



MORE INFORMATION
To poll for changes to the Active Directory, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a new Visual C# Console Application project.

By default, the Class1.cs file is created in Visual Studio .NET, and the Program.cs file is created in Visual Studio 2005. Click Project, and then click Add References.

The Add References dialog box appears. Click System.DirectoryServices.dll. Click the COM tab. Click Active DS Type Library. Click OK to add System.DirectoryServices and ADSI library to the current project as references.</li>  Replace the auto-generated code in the Class1.cs or in the Program.cs file with the following code: // Use Visual C# .NET to poll for changes to the Active Directory.

using System; using System.DirectoryServices; using ActiveDs;

namespace ConsoleApplication1 {   class Class1 {       [STAThread] static void Main(string[] args) {           //TODO: Set UserName, DomainName, and UpperLevelDomain to reflect your environment. PollADObject( &quot;LDAP://cn=UserName,cn=Users,dc=DomainName,dc=UpperLevelDomain&quot; ); }       static void PollADObject(string strUserADSPath ) {           DirectoryEntry oUser = new DirectoryEntry(strUserADSPath); string strUserName = oUser.Name ; IADsLargeInteger li_ad = (IADsLargeInteger)oUser.Properties[&quot;USNChanged&quot;].Value ; long l_uChanged = GetLongFromLargeInteger( li_ad ); System.Console.WriteLine( l_uChanged.ToString+&quot; &quot; + strUserName ); Console.ReadLine; }

static long GetLongFromLargeInteger( IADsLargeInteger  Li ) {           long retval = Li.HighPart; retval <<=32; retval |=(uint)Li.LowPart; return retval; }   } } </li> Search for the TODO text string in the previous sample code. Modify the sample code for your environment.</li> Press F5 to compile and to run the application.</li></ol>

<div class="references_section">