Dim RootDE As DirectoryEntry = New DirectoryEntry("LDAP://Domain Name/DC=cn,DC=Domain Name,DC=com", "UserName", "Pwd") '必须要加入用户身份认证
Dim Searcher As DirectorySearcher = New DirectorySearcher(RootDE)
Dim CollectionResult As SearchResultCollection
Dim i As Integer
CollectionResult = Nothing
Try
Searcher.Filter = "(&(objectClass=User)(mailNickname=*)(cn=用户名))" Searcher.PropertiesToLoad.Add("mailNickname")
Searcher.PropertiesToLoad.Add("ADsPath")
Searcher.PropertiesToLoad.Add("Name")
Searcher.PropertiesToLoad.Add("Mail")
Searcher.Sort.Direction = SortDirection.Ascending
MessageBox.Show(Searcher.FindAll.Count)
CollectionResult = Searcher.FindAll
Dim lop As Integer
For lop = 0 To CollectionResult.Count - 1
MessageBox.Show(CollectionResult.Item(lop).Properties.Count)
TreeView1.Nodes.Add(CollectionResult.Item(lop).Properties("Name").Item(0))
Next
把AD的信息写到tree:
Imports System.DirectoryServices
Private Sub GetADInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim RootPath As String "
Dim dataxtong As New CommonSelf()
RootPath = "LDAP://" + dataxtong.GetSystemData("RootOU") + "," + dataxtong.GetSystemData("DomainName")
Dim RootDE As DirectoryEntry = New DirectoryEntry(RootPath)
Dim Searcher As DirectorySearcher = New DirectorySearcher(RootDE)
Dim CollectionResult As SearchResultCollection
Dim TempNode, TempNode1 As TreeNode
Dim i As Integer
CollectionResult = Nothing
Try
Searcher.Filter = "(&(objectClass=OrganizationalUnit))" '& "(OU=" & Trim(EnterpriseName) & "))"
Searcher.PropertiesToLoad.Add("Description")
Searcher.PropertiesToLoad.Add("ADsPath")
Searcher.PropertiesToLoad.Add("Name")
Searcher.Sort.Direction = SortDirection.Ascending
CollectionResult = Searcher.FindAll()
If CollectionResult.Count <= 1 Then
MsgBox("无组织单位。")
Else
For i = 1 To CollectionResult.Count - 1
If Len(CollectionResult.Item(i).Properties("Description").Item(0).ToString) <> 13 Then
MsgBox("单位:" & CollectionResult.Item(i).Properties("Name").Item(0).ToString & "的编码有误。")
Else
If Mid(Trim(CollectionResult.Item(i).Properties("Description").Item(0).ToString), 11, 3) = "000" Then
TempNode = Me.TreeView1.Nodes.Add(CollectionResult.Item(i).Properties("Name").Item(0).ToString)
TempNode.ImageIndex = 0
TempNode.Tag = CollectionResult.Item(i).Properties("Description").Item(0).ToString & CollectionResult.Item(i).Path
Else
TempNode1 = TempNode.Nodes.Add(CollectionResult.Item(i).Properties("Name").Item(0).ToString)
TempNode1.ImageIndex = 1
TempNode1.Tag = CollectionResult.Item(i).Properties("Description").Item(0).ToString & CollectionResult.Item(i).Path
End If
End If
Next
End If
Catch
MsgBox("查询系统目录出现错误。")
Me.TreeView1.Nodes.Clear()
CollectionResult = Nothing
RootDE.Dispose()
Searcher.Dispose()
RootDE = Nothing
Searcher = Nothing
End Try
End Sub