我对这段代码有问题......显然,它可以工作,但对我来说太慢了,有人有什么想法吗?当我尝试访问以下内容时它会变慢
Dim u As UserPrincipal = UserPrincipal.FindByIdentity(ctx, p.SamAccountName)
同样,以下代码可以正常工作,但速度很慢。如果我取出上面的一段代码并p.SamAccountName
在 1 秒内完成搜索,那么我确定我做错了什么。
Dim sw As New Stopwatch
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
sw.Start()
DataGridView1.ColumnCount = 3
DataGridView1.Columns(0).Name = "Account Name"
DataGridView1.Columns(1).Name = "First Name"
DataGridView1.Columns(2).Name = "Last Name"
Dim ctx = New PrincipalContext(ContextType.Domain, "JOI", DomainName)
Dim userPrin As New UserPrincipal(ctx)
userPrin.Name = "*"
Dim searcher = New System.DirectoryServices.AccountManagement.PrincipalSearcher()
searcher.QueryFilter = userPrin
Dim results = searcher.FindAll()
For Each p As Principal In results
Dim u As UserPrincipal = UserPrincipal.FindByIdentity(ctx, p.SamAccountName)
Dim row As String() = New String() {u.SamAccountName, u.GivenName, u.Surname}
DataGridView1.Rows.Add(row)
Next
sw.Stop()
MessageBox.Show("Finished in :" & sw.Elapsed.Duration.Seconds & " seconds")
End Sub
Private Shared Function DomainName() As String
Dim objRootDSE As New DirectoryEntry("LDAP://RootDSE")
DomainName = objRootDSE.Properties("defaultNamingContext")(0)
End Function