如何填充树视图以仅显示文件夹及其子目录

时间:2014-05-13 19:11:29

标签: vb.net treeview

我将非常感谢你对此的帮助。 我试图填充树视图,只显示一个目录作为其主根和子目录。其他在线代码和教程显示驱动器和其他特殊文件夹。

我想只显示一个文件夹路径: 节点中的C:\ Main Folder \ Subdirectory1 \ Subdirectory2等

这就是我所拥有的,而且没有帮助。

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)
    'Get a list of drives
    Dim drives As DriveInfo() = DriveInfo.GetDrives()
    Dim rootDir As String = String.Empty
    'Now loop thru each drive and populate the treeview
    For i As Integer = 0 To drives.Length - 1
        rootDir = drives(i).Name
        'Add this drive as a root node
        TreeView1.Nodes.Add(rootDir)
        'Populate this root node
        PopulateTreeView(rootDir, TreeView1.Nodes(i))
    Next

End Sub
Private Sub PopulateTreeView(dir As String, parentNode As TreeNode)
    Dim folder As String = String.Empty
    Try
        Dim folders As String() = System.IO.Directory.GetDirectories(dir)
        If folders.Length <> 0 Then
            Dim childNode As TreeNode = Nothing
            For Each folder_loopVariable As String In folders
                folder = folder_loopVariable
                childNode = New TreeNode(folder)
                childNode.Nodes.Add("")
                parentNode.Nodes.Add(childNode)
            Next
        End If
        Dim files As String() = System.IO.Directory.GetFiles(dir)
        If files.Length <> 0 Then
            Dim childNode As TreeNode = Nothing
            For Each file As String In files
                childNode = New TreeNode(file)
                parentNode.Nodes.Add(childNode)
            Next
        End If
    Catch ex As UnauthorizedAccessException
        parentNode.Nodes.Add(folder & Convert.ToString(": Access Denied"))
    End Try
End Sub

1 个答案:

答案 0 :(得分:1)

他tomado como basetucódigyhe hecho algunas modificaciones para poder adaptarlo a mis necesidades。

Si necesitas crear una lista de todos los archivos por unidades,la funcion ActualizaTV es la encargada de Actualizar el TreeView que,en mi caso,se llama tvDir

Te dejo el resultado por si te viene bien:

Sub ActualizaTV(workpath As String)
    If workpath Is Nothing Then Exit Sub
    With tvDir
        .Nodes.Clear()

        'Creating the root node
        .Nodes.Add(New TreeNode(workpath))
        PopulateTreeView(workpath, .Nodes(0))

    End With
End Sub

Sub PopulateTreeView(dir As String, parentNode As TreeNode)

    Try
        'Se añada en primer lugar los archivos del directorio
        For Each file In System.IO.Directory.GetFiles(dir)
            If file.Length = 0 Then Continue For
            parentNode.Nodes.Add(New TreeNode(file.Replace(dir & "\", "")))
        Next

        'Se buscan las posibles carpetas nuevas del directorio y se añadem
        For Each folder As String In System.IO.Directory.GetDirectories(dir)
            If folder.Length = 0 Then Continue For
            parentNode.Nodes.Add(New TreeNode(folder))

            'En caso de que haya subcarpetas, se repite la operación
            If UBound(System.IO.Directory.GetDirectories(folder)) > 0 Or _
                UBound(System.IO.Directory.GetFiles(folder)) > 0 Then _
                    PopulateTreeView(folder, parentNode.LastNode)

        Next

    Catch ex As UnauthorizedAccessException
        parentNode.Nodes.Add(dir & Convert.ToString(": Access Denied"))
    End Try
End Sub

Un saludo