Active Directory Sample I


This sample shows how to access Active Directory information in a Windows 2000 domain.

My test network consists of a Windows 2000 domain controller and a Windows 2000 server. When you install a Windows 2000 domain controller and are creating a new domain you install both the active directory and DNS which the active directory uses. The Active Directory in Windows 2000 allows you to store information about servers, printers, users etc.

So finally I thought yeah very nice, wonder if I can run code from my server to access the directory information. Below is the code which lists all the entries in my small active Directory. Note my domain is called Microsoft for testing purposes.

Source Code:

// Active Directory Sample 1
// Lists all entries in the Active Directory for Domain microsoft.
using
System;
using
System.DirectoryServices;
namespace
ActiveDirectory
{
///
<summary>
///
Summary description for Class1.
/// </summary>

class
Class1
{
static void Main (string
[] args)
{
//Note : microsoft is the name of my domain for testing purposes.
DirectoryEntry entry = new
DirectoryEntry(LDAP://microsoft);
System.DirectoryServices.DirectorySearcher mySearcher =
new
System.DirectoryServices.DirectorySearcher(entry);
mySearcher.Filter = ("(objectClass=*)");
Console.WriteLine("Active Directory Information");
Console.WriteLine("===========================================");
foreach(System.DirectoryServices.SearchResult resEnt in
mySearcher.FindAll())
{
Console.WriteLine( resEnt.GetDirectoryEntry().Name.ToString() );
Console.WriteLine( resEnt.GetDirectoryEntry().Path.ToString() );
Console.WriteLine( resEnt.GetDirectoryEntry().NativeGuid.ToString() );
Console.WriteLine("===========================================");
}
}
}
}