Imports System
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Public Class DirectoryTreeView
Inherits TreeView
Sub New()
'Make a little more room for long directory names.
Me.Width *= 2
'get images for tree
'You need to put these images from the samples into your
'project directory for this part to work.
Me.ImageList = New ImageList()
Me.ImageList.Images.Add(New Bitmap("35FLOPPY.BMP"))
Me.ImageList.Images.Add(New Bitmap("CLSDFOLD.BMP"))
Me.ImageList.Images.Add(New Bitmap("OPENFOLD.BMP"))
'Contruct tree
Me.RefreshTree()
End Sub
Public Sub RefreshTree()
'Turn off visual updating and clear tree.
Me.BeginUpdate()
Me.Nodes.Clear()
'Make disk drives the root node
Dim astrDrives() As String = Directory.GetLogicalDrives()
Dim str As String 'helper for For Each loop
For Each str In astrDrives
Dim tnDrive As New TreeNode(str, 0, 0)
Me.Nodes.Add(tnDrive)
Me.AddDirectories(tnDrive)
If (str = "C:\\") Then
Me.SelectedNode = tnDrive
End If
Next
Me.EndUpdate()
End Sub
Sub AddDirectories(ByVal tn As TreeNode)
tn.Nodes.Clear()
Dim strPath As String
Dim dirInfo As New DirectoryInfo(strPath)
Dim adirInfo() As DirectoryInfo
Try
adirInfo = dirInfo.GetDirectories()
Catch
Return
End Try
Dim di As DirectoryInfo 'helper for For Each loop
For Each di In adirInfo
Dim tnDir As New TreeNode(di.Name, 1, 2)
tn.Nodes.Add(tnDir)
'We could now fill up the whole tree with this statement:
' AddDirectories(tnDir);
'But it would be too slow. Try it!
Next
End Sub
Protected Overrides Sub OnBeforeExpand(ByVal tvcea As System.Windows.Forms.TreeViewCancelEventArgs)
MyBase.OnBeforeExpand(tvcea)
Me.BeginUpdate()
Dim tn As TreeNode 'helper for For Each loop
For Each tn In tvcea.Node.Nodes
Me.AddDirectories(tn)
Next
Me.EndUpdate()
End Sub
End Class