我自己写的一个继承自treeview的类,大家看看,或许有点帮助 Imports System Imports System.Drawing Imports System.IO Imports System.Windows.Forms Imports System.Text.RegularExpressionsImports System.Threading Public Class DirectoryTreeView Inherits TreeView Private tn As myTreeNode Private mRootNode As myTreeNode Sub New() ImageList = New ImageList '在imagelist中加入一系列图片 ImageList.Images.AddStrip(New Bitmap(Me.GetType(), "FolderViewerImages.bmp")) ImageList.TransparentColor = Color.Cyan '设置透明色 ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf RefreshTree)) 'RefreshTree() End Sub Sub RefreshTree(ByVal o As Object) BeginUpdate() Nodes.Clear() Dim tnDesktop As New myTreeNode("桌面", 0, 0) Nodes.Add(tnDesktop) Dim tnMycomputer As New myTreeNode("我的电脑", 2, 2) Nodes.Add(tnMycomputer) tnMycomputer.Nodes.Clear() Dim astrDrives() As String = Directory.GetLogicalDrives() Dim str As String For Each str In astrDrives Dim tnDrive As New myTreeNode(str.Substring(0, 2), 6, 6) tnMycomputer.Nodes.Add(tnDrive) Next str tnMycomputer.Expand() Dim strMydocument As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) Dim tnMydocument As New myTreeNode("我的文档", 1, 1) Nodes.Add(tnMydocument) AddDirectories(tnMydocument) EndUpdate() End Sub Private Sub AddDirectories(ByVal tn As myTreeNode) tn.Nodes.Clear() Dim strPath As String = tn.FullPath & "\" Dim dirinfo As New DirectoryInfo(strPath) Dim adirinfo() As DirectoryInfo If Not dirinfo.Exists Then Return Try adirinfo = dirinfo.GetDirectories() Catch Return End Try Dim di As DirectoryInfo For Each di In adirinfo If di.Name.Equals("Recycled") Then Dim tnDir As New myTreeNode(di.Name, 10, 10) tn.Nodes.Add(tnDir) Else Dim tnDir As New myTreeNode(di.Name, 4, 5) tn.Nodes.Add(tnDir) End If Next di End Sub Protected Overrides Sub OnBeforeExpand(ByVal tvcea As TreeViewCancelEventArgs) MyBase.OnBeforeExpand(tvcea) For Each tn In tvcea.Node.Nodes AddDirectories(tn) Next tn End Sub Public Property RootNode() As myTreeNode Get Return mRootNode End Get Set(ByVal Value As myTreeNode) mRootNode = Value End Set End PropertyEnd Class
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Imports System.Text.RegularExpressionsImports System.Threading
Public Class DirectoryTreeView
Inherits TreeView
Private tn As myTreeNode Private mRootNode As myTreeNode Sub New()
ImageList = New ImageList
'在imagelist中加入一系列图片
ImageList.Images.AddStrip(New Bitmap(Me.GetType(), "FolderViewerImages.bmp"))
ImageList.TransparentColor = Color.Cyan '设置透明色
ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf RefreshTree))
'RefreshTree()
End Sub Sub RefreshTree(ByVal o As Object) BeginUpdate()
Nodes.Clear() Dim tnDesktop As New myTreeNode("桌面", 0, 0)
Nodes.Add(tnDesktop) Dim tnMycomputer As New myTreeNode("我的电脑", 2, 2)
Nodes.Add(tnMycomputer)
tnMycomputer.Nodes.Clear() Dim astrDrives() As String = Directory.GetLogicalDrives()
Dim str As String
For Each str In astrDrives
Dim tnDrive As New myTreeNode(str.Substring(0, 2), 6, 6)
tnMycomputer.Nodes.Add(tnDrive) Next str
tnMycomputer.Expand() Dim strMydocument As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) Dim tnMydocument As New myTreeNode("我的文档", 1, 1)
Nodes.Add(tnMydocument)
AddDirectories(tnMydocument) EndUpdate()
End Sub Private Sub AddDirectories(ByVal tn As myTreeNode) tn.Nodes.Clear()
Dim strPath As String = tn.FullPath & "\"
Dim dirinfo As New DirectoryInfo(strPath)
Dim adirinfo() As DirectoryInfo
If Not dirinfo.Exists Then Return
Try
adirinfo = dirinfo.GetDirectories()
Catch
Return
End Try
Dim di As DirectoryInfo
For Each di In adirinfo
If di.Name.Equals("Recycled") Then
Dim tnDir As New myTreeNode(di.Name, 10, 10)
tn.Nodes.Add(tnDir)
Else
Dim tnDir As New myTreeNode(di.Name, 4, 5)
tn.Nodes.Add(tnDir)
End If
Next di End Sub Protected Overrides Sub OnBeforeExpand(ByVal tvcea As TreeViewCancelEventArgs)
MyBase.OnBeforeExpand(tvcea) For Each tn In tvcea.Node.Nodes AddDirectories(tn)
Next tn
End Sub
Public Property RootNode() As myTreeNode
Get
Return mRootNode
End Get
Set(ByVal Value As myTreeNode)
mRootNode = Value
End Set
End PropertyEnd Class