在TreeView中显示文件夹/文件(VB.NET 2008)

时间:2015-10-27 14:01:12

标签: vb.net winforms treeview

我需要创建一个树视图,并从一个路径中获取所有目录和文件。

我无法编译此代码。问题出在这一行:mnodedirectory.nodes.add(mfilesnode)

PATH

2 个答案:

答案 0 :(得分:1)

您已使用mnodedirectory作为TreeView的名称,其中它是DirectoryInfo对象的名称。

这是一个从按钮单击调用的示例,其中c:\ temp \作为给定的起始目录

Private Sub TestButton_Click(sender As System.Object, e As System.EventArgs) Handles Button39.Click

    TestTreeView.Nodes.Clear()
    Dim ndParent As TreeNode = TestTreeView.Nodes.Add("c:\temp\")
    ndParent.Tag = "c:\temp"
    'add a child node to allow 'expand' to fire
    ndParent.Nodes.Add("*temp*")

End Sub

Private Sub populateFilesAndFolders(parentNode As TreeNode, startingPath As String)

    Dim inspectDirectoryInfo As IO.DirectoryInfo = New IO.DirectoryInfo(startingPath)
    ' add each subdirectory from the file system to the expanding node as a child node
    For Each directoryInfoItem As IO.DirectoryInfo In inspectDirectoryInfo.GetDirectories
        ' declare a child treenode for the next subdirectory
        Dim directoryTreeNode As New TreeNode
        ' store the full path to this directory in the child treenode's tag property
        directoryTreeNode.Tag = directoryInfoItem.FullName
        ' set the child treenodes's display text
        directoryTreeNode.Text = directoryInfoItem.Name
        ' add a dummy treenode to this child treenode to make it expandable
        directoryTreeNode.Nodes.Add("*temp*")
        ' add this child treenode to the expanding treenode
        parentNode.Nodes.Add(directoryTreeNode)
        populateFilesAndFolders(directoryTreeNode, directoryInfoItem.FullName)

    Next

    For Each fileItem As IO.FileInfo In inspectDirectoryInfo.GetFiles
        Dim fileNode As New TreeNode
        fileNode.Tag = fileItem.FullName
        fileNode.Text = fileItem.Name
        parentNode.Nodes.Add(fileNode)
    Next

End Sub

Private Sub TestTreeView_BeforeExpand(sender As System.Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TestTreeView.BeforeExpand
    Try
        populateFilesAndFolders(e.Node, e.Node.Tag.ToString)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

答案 1 :(得分:0)

请先反复询问,然后再问This

'Don't forget to import this
Imports System.IO

'Declare these
Private Enum ItemType
    Drive
    Folder
    File
End Enum

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each drive As DriveInfo In DriveInfo.GetDrives
        Dim node As TreeNode = _
            file_view_tree.Nodes.Add(drive.Name)
        node.Tag = ItemType.Drive
        node.Nodes.Add("FILLER")
    Next
End Sub

Private Sub file_view_tree_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles file_view_tree.BeforeExpand
    Dim currentNode As TreeNode = e.Node
    currentNode.Nodes.Clear()
    Try
        'Now go get all the files and folders
        Dim fullPathString As String = currentNode.FullPath

        'Handle each folder
        For Each folderString As String In _
            Directory.GetDirectories(fullPathString)
            Dim newNode As TreeNode = _
            currentNode.Nodes.Add(Path.GetFileName(folderString))
            Dim x As String = Path.GetFileName("")
            newNode.Tag = ItemType.Folder
            newNode.Nodes.Add("FILLER")
        Next

        'Handle each file
        For Each fileString As String In _
            Directory.GetFiles(fullPathString)
            'Get just the file name portion (without the path) :
            Dim newNode As TreeNode = _
            currentNode.Nodes.Add(Path.GetFileName(fileString))
            newNode.Tag = ItemType.File
        Next
    Catch ex As Exception

    End Try
End Sub