我有一個組合框綁定到一個數據表,我用活動目錄中的用戶名填寫。此代碼需要大約一分鐘才能完成。有沒有更好的方法,我失蹤了?填寫WPF組合框與Active Directory用戶需要的時間太長
Function users() As DataTable
Dim dt As DataTable
Dim dr As DataRow
Dim idCoulumn As DataColumn
Dim nameCoulumn As DataColumn
Dim dirEntry As New System.DirectoryServices.DirectoryEntry("LDAP://CN=Users,DC=myDomain,DC=local")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
Dim oResults As SearchResultCollection
oSearcher.PropertiesToLoad.Add("samAccountName")
oSearcher.PropertiesToLoad.Add("givenname")
oSearcher.PropertiesToLoad.Add("sn")
oSearcher.PropertiesToLoad.Add("cn")
oSearcher.Filter = "objectCategory=person"
oResults = oSearcher.FindAll
dt = New DataTable()
idCoulumn = New DataColumn("ID", Type.GetType("System.String"))
nameCoulumn = New DataColumn("Name", Type.GetType("System.String"))
dt.Columns.Add(idCoulumn)
dt.Columns.Add(nameCoulumn)
For Each oResult In oResults
With oResult.GetDirectoryEntry()
If .Properties("cn").Value <> "" AndAlso .Properties("samAccountName").Value <> "" AndAlso .Properties("sn").Value <> "" Then
dr = dt.NewRow()
dr("ID") = .Properties("samAccountName").Value
dr("Name") = String.Format("{0},{1} : {2}", .Properties("sn").Value, .Properties("givenname").Value, .Properties("samAccountName").Value)
dt.Rows.Add(dr)
End If
End With
Next
dt.DefaultView.Sort = "Name"
Return dt
End Function
太棒了!這不是完全的答案,但它確實幫助我到達那裏。謝謝。 – donL