Hi,
I started off a project to build my own "AD explorer", and think your are
after something similar. The code below build a treeview representation of
AD. See if this gives you some help
Regards
Niclas
Imports System.DirectoryServices
Public Class Form1
Inherits System.Windows.Forms.Form
Private sRootDSE As String
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
sRootDSE = GetRootDSE()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.TreeView1 = New System.Windows.Forms.TreeView
Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
Me.SuspendLayout()
'
'TreeView1
'
Me.TreeView1.ImageList = Me.ImageList1
Me.TreeView1.Location = New System.Drawing.Point(32, 24)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.Size = New System.Drawing.Size(240, 248)
Me.TreeView1.TabIndex = 0
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
Me.ImageList1.ImageStream =
CType(resources.GetObject("ImageList1.ImageStream" ),
System.Windows.Forms.ImageListStreamer)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(552, 302)
Me.Controls.Add(Me.TreeView1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Sub GetOU()
Dim root As New DirectoryEntry("LDAP://" & sRootDSE)
Dim searcher As New DirectorySearcher(root)
Dim s As SearchResult
Dim nodearr() As TreeNode
searcher.SearchScope = SearchScope.OneLevel
Dim I As Integer
Dim cp As IComparer
cp = New NodeComparer
ReDim nodearr(searcher.FindAll.Count - 1)
For Each s In searcher.FindAll
Dim no As New
TreeNode(s.GetDirectoryEntry.Properties("Name").Va lue.ToString,
GetObjectClass(s.GetDirectoryEntry.Properties("Dis tinguishedName").Value.ToString),
GetObjectClass(s.GetDirectoryEntry.Properties("Dis tinguishedName").Value.ToString))
no.Tag =
s.GetDirectoryEntry.Properties("DistinguishedName" ).Value.ToString
nodearr(I) = no
I += 1
Next
Array.Sort(nodearr, cp)
Dim k As TreeNode
TreeView1.Nodes.AddRange(nodearr)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
GetOU()
End Sub
Private Sub TreeView1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TreeView1.DoubleClick
If TreeView1.SelectedNode.GetNodeCount(True) < 1 Then
Dim root As New DirectoryEntry("LDAP://" &
TreeView1.SelectedNode.Tag.ToString)
Dim searcher As New DirectorySearcher(root)
Dim s As SearchResult
Dim nodearr As TreeNode()
searcher.SearchScope = SearchScope.OneLevel
Dim I As Integer
Dim cp As IComparer
cp = New NodeComparer
ReDim nodearr(searcher.FindAll.Count - 1)
For Each s In searcher.FindAll
Dim no As New
TreeNode(s.GetDirectoryEntry.Properties("Name").Va lue.ToString,
GetObjectClass(s.GetDirectoryEntry.Properties("Dis tinguishedName").Value.ToString),
GetObjectClass(s.GetDirectoryEntry.Properties("Dis tinguishedName").Value.ToString))
no.Tag =
s.GetDirectoryEntry.Properties("DistinguishedName" ).Value.ToString
nodearr(I) = no
I += 1
Next
Array.Sort(nodearr, cp)
TreeView1.SelectedNode.Nodes.AddRange(nodearr)
TreeView1.SelectedNode.Expand()
End If
End Sub
Private Function GetRootDSE() As String
Dim RootEntry As New DirectoryEntry("LDAP://RootDSE")
Return RootEntry.Properties("DefaultNamingContext").Value .ToString
'Add error handling
End Function
Private Function GetObjectClass(ByVal DN As String) As Integer
Dim m As New DirectoryEntry("LDAP://" & DN)
Dim r As New DirectoryEntry(m.NativeObject.schema.ToString)
Select Case r.Name
Case "organizationalUnit"
Return 0
Case "computer"
Return 1
Case "user"
Return 2
Case Else
Return 0
End Select
End Function
End Class
Class NodeComparer
Implements IComparer 'Implement the IComparer Interface
Overloads Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements IComparer.Compare
Return CType(x, TreeNode).ImageIndex - CType(y, TreeNode).ImageIndex
End Function
End Class
"Director - Minvent" <Di*************@discussions.microsoft.com> wrote in
message news:D1**********************************@microsof t.com...
Hi,
I am trying to work out the easiest way to add some things to a treeview.
I have got a list of the priviliged names for each of the computers e.g.
LDAP://DomainName/CN=ComputerName,OU=OrgUnit1,OU=OrgUnit2,
DC=DCRecord1,DC=DCRecord2
I am iterating through a list of these names to split them into strings
for
the domain name and common name and an arraylist of strings for the
organisational units.
That bit works fine no problems, The problem I have is that on a one by
one
basis I need to add them onto the correct node of the treeview taking
account
the current structure of domains and OU's in the tree view and not
reproducing nodes that are already present.
Hope that makes sense!
Thanks M
--
If at first you don't succeed... Hide the evidence that you tried!