可以用TreeView(Microsoft Commom Control里的),也可以用API,但是微软似乎没有提供comclt32.dll里的函数声明。

解决方案 »

  1.   

    Private Sub frmDirDemo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim oNode As New System.Windows.Forms.TreeNode()
            Dim rootDir As String
            Try
                oNode.ImageIndex = 0
                oNode.SelectedImageIndex = 0
                oNode.Text = "我的电脑"
                TreeView1.Nodes.Add(oNode)
                Dim xx, i
                For Each rootDir In Directory.GetLogicalDrives
                    Dim subNode As New System.Windows.Forms.TreeNode()
                    xx = Split(rootDir, "\")
                    subNode.Text = xx(0)
                    oNode.Nodes.Insert(i, subNode)
                    oNode.Nodes.Item(i).Nodes.Add("")
                    i = i + 1
                Next
            Catch ex As Exception
                MsgBox("Cannot create initial node:" & ex.ToString)
                End
            End Try
        End Sub
        Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
            If e.Node.ImageIndex = 2 Then Exit Sub        Try
                If e.Node.GetNodeCount(False) = 1 And e.Node.Nodes(0).Text = "" Then
                    e.Node.Nodes(0).Remove()
                    EnumerateChildren(e.Node)
                End If
            Catch ex As Exception
                MsgBox("Unable to expand " & e.Node.FullPath & ":" & ex.ToString)
            End Try        If e.Node.GetNodeCount(False) > 0 Then
                e.Node.ImageIndex = 1
                e.Node.SelectedImageIndex = 1
            End If    End Sub    Private Sub EnumerateChildren(ByVal oParent As System.Windows.Forms.TreeNode)        Dim xx As String = oParent.FullPath
            xx = Replace(xx, "我的电脑\", "")
            Dim oFS As New DirectoryInfo(xx & "\")
            Dim oDir As DirectoryInfo
            Dim oFile As FileInfo        Try
                For Each oDir In oFS.GetDirectories()
                    Dim oNode As New System.Windows.Forms.TreeNode()
                    oNode.Text = oDir.Name
                    oNode.ImageIndex = 0
                    oNode.SelectedImageIndex = 0
                    oParent.Nodes.Add(oNode)
                    oNode.Nodes.Add("")
                Next
            Catch ex As Exception
                MsgBox("Cannot list folders of " & oParent.FullPath & ":" & ex.ToString)
            End Try        Try
                For Each oFile In oFS.GetFiles()
                    Dim oNode As New System.Windows.Forms.TreeNode()
                    oNode.Text = oFile.Name & " (" & oFile.Length & " bytes)"
                    oNode.ImageIndex = 2
                    oNode.SelectedImageIndex = 2
                    oParent.Nodes.Add(oNode)
                Next
            Catch ex As Exception
                MsgBox("Cannot list files in " & oParent.FullPath & ":" & ex.ToString)
            End Try    End Sub    Private Sub TreeView1_BeforeCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCollapse
            e.Node.ImageIndex = 0
            e.Node.SelectedImageIndex = 0
        End Sub