Using Active Directory in VB.NET

 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.
' Written by J O'Donnell , 22/08/01 

Imports System
Imports System.DirectoryServices
Namespace
 ActiveDirectory
'/ <summary>
'/ Summary description for Class1.
'/ </summary>
Class Class1
Shared Sub Main(ByVal args() As String)
'Note : microsoft is the name of my domain for testing purposes.
Dim enTry As DirectoryEnTry = New DirectoryEnTry(LDAP://microsoft)
System.DirectoryServices.DirectorySearcher mySearcher = 
New
System.DirectoryServices.DirectorySearcher(enTry)
mySearcher.Filter = ("(objectClass=*)")
Console.WriteLine("Active Directory Information")
Console.WriteLine("===========================================")
For Each (System.DirectoryServices.SearchResult resEnt)
Console.WriteLine(resEnt.GetDirectoryEnTry().Name.ToString())
Console.WriteLine(resEnt.GetDirectoryEnTry().Path.ToString())
Console.WriteLine(resEnt.GetDirectoryEnTry().NativeGuid.ToString())
Console.WriteLine("===========================================")
Next
End Sub
End
 
Class
End
 Namespace

Up Next
    Ebook Download
    View all
    Learn
    View all