Sub FillListbox(ByRef ListBox As ListBox, ByVal OU As String)
Try
Dim i As Integer = 0
Dim entry As New DirectoryEntry("LDAP://" & OU)
Dim searcher As New DirectorySearcher(entry)
searcher.Filter = "(objectClass=*)"
Dim rangeStep As Integer = 1500
Dim rangeLow As Integer = 0
Dim rangeHigh As Integer = rangeLow + (rangeStep - 1)
Dim lastQuery As Boolean = False
Dim quitLoop As Boolean = False
Do
Dim attributeWithRange As String
If Not lastQuery Then
attributeWithRange = [String].Format("member;range={0}-{1}", rangeLow, rangeHigh)
Else
attributeWithRange = [String].Format("member;range={0}-*", rangeLow)
End If
searcher.PropertiesToLoad.Clear()
searcher.PropertiesToLoad.Add(attributeWithRange)
searcher.PropertiesToLoad.Add("samaccountname")
searcher.PropertiesToLoad.Add("CN")
Dim results As SearchResult = searcher.FindOne()
If results.Properties.Contains(attributeWithRange) Then
For Each obj As Object In results.Properties(attributeWithRange)
Dim DirEntry As New DirectoryEntry("LDAP://" & obj.ToString())
ListBox.Items.Add(DirEntry.Properties("samaccountname").Value & " | " & DirEntry.Properties("cn").Value)
Next
If lastQuery Then
quitLoop = True
End If
Else
lastQuery = True
End If
If Not lastQuery Then
rangeLow = rangeHigh + 1
rangeHigh = rangeLow + (rangeStep - 1)
End If
Loop While Not quitLoop
Catch ex As Exception
MessageBox.Show("Error " & ex.Message, "ERROR Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub