Imports System.Data
Imports System.DirectoryServices
Public Class Form1
Dim objpath As DirectoryEntry
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
objpath = New DirectoryEntry("LDAP://OU=Accounts,DC=Test,DC=COM")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(objpath)
Dim oResults As SearchResultCollection
Dim oResult As SearchResult
oSearcher.PropertiesToLoad.Add("name")
oSearcher.PropertiesToLoad.Add("cn")
' Set the search scope
oSearcher.SearchScope = "OneLevel"
oResults = oSearcher.FindAll
For Each oResult In oResults
' CN will never be "", it's a mandatory attribute in AD
' If oResult.Properties("cn")(0) = "" Then
' Properties are stored in a collection, single value properties like name,
' and CN are stored at index (0) in the collection.
MessageBox.Show(oResult.Properties("name")(0))
ListBox1.Items.Add(oResult.Properties("name")(0))
' End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message + " " + ex.StackTrace)
End Try
End Sub
End Class
|