DetailPage-MSS-KB

Microsoft small business knowledge base

Article ID: 326091 - Last Review: May 23, 2007 - Revision: 2.5

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:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. 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.
  3. Click Project, and then click Add References.

    The Add References dialog box appears.
  4. Click System.DirectoryServices.dll.
  5. Click the COM tab.
  6. Click Active DS Type Library.
  7. Click OK to add System.DirectoryServices and ADSI library to the current project as references.
  8. 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( "LDAP://cn=UserName,cn=Users,dc=DomainName,dc=UpperLevelDomain" );
    		}
        
    		static void PollADObject(string strUserADSPath )
    		{
    			DirectoryEntry oUser = new DirectoryEntry(strUserADSPath);
    			string strUserName = oUser.Name ;					 
    			IADsLargeInteger li_ad = (IADsLargeInteger)oUser.Properties["USNChanged"].Value ;
    			long l_uChanged = GetLongFromLargeInteger( li_ad );
    			System.Console.WriteLine( l_uChanged.ToString()+"  " + strUserName );
    			Console.ReadLine();
    		}
    
    		static long GetLongFromLargeInteger(  IADsLargeInteger  Li )
    		{
    			long retval = Li.HighPart;
    			retval <<=32;
    			retval |=(uint)Li.LowPart;
    			return retval;
    		}
    	}
    }
    
  9. Search for the TODO text string in the previous sample code. Modify the sample code for your environment.
  10. Press F5 to compile and to run the application.

REFERENCES

For additional information about tracking changes in the Active Directory, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/ms677974.aspx (http://msdn2.microsoft.com/en-us/library/ms677974.aspx)

For additional information about tracking changes in the Active Directory, see the "Tracking Changes" topic in the "Using Active Directory" section of the Platform SDK.

APPLIES TO
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbhowto KB326091
Share
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support