在 TreeView ( VB.NET 2008 ) 中显示文件夹/文件 [英] Show folders/files in TreeView ( VB.NET 2008 )

查看:29
本文介绍了在 TreeView ( VB.NET 2008 ) 中显示文件夹/文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个树视图,并将我所有的目录和文件放在一个路径中.

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

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

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

推荐答案

请先搜索再问一遍这个

'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

这篇关于在 TreeView ( VB.NET 2008 ) 中显示文件夹/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆