简体   繁体   中英

Show folders/files in TreeView ( VB.NET 2008 )

I need to create one treeview and to have there all of my directories and files from one path.

I cant compile this code. The problem is on this line : mnodedirectory.nodes.add(mfilesnode)

' get the directory representing this node

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

For Each mfile As IO.FileInfo In mnodedirectory.GetFiles
    Dim mfilesnode As New TreeNode
    mfilesnode.Tag = mfile.FullName
    mfilesnode.Text = mfile.Name
    mnodedirectory.nodes.add(mfilesnode)
Next

You've used mnodedirectory as the name of your TreeView, where it is the name of a DirectoryInfo object.

This is an example called from a button click, with c:\\temp\\ as the given starting directory

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

Please search first before ask again and again 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM