Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在树状视图中显示文件夹/文件(VB.NET 2008)_Vb.net_Winforms_Treeview - Fatal编程技术网

在树状视图中显示文件夹/文件(VB.NET 2008)

在树状视图中显示文件夹/文件(VB.NET 2008),vb.net,winforms,treeview,Vb.net,Winforms,Treeview,我需要创建一个树状视图,并有我的所有目录和文件从一个路径 我无法编译这段代码。问题出在这一行:mnodedirectory.nodes.add(mfilesnode) 您已经使用mnodedirectory作为TreeView的名称,其中它是DirectoryInfo对象的名称 这是一个通过单击按钮调用的示例,其中c:\temp\作为给定的起始目录 Private Sub TestButton_Click(sender As System.Object, e As System.EventArg

我需要创建一个树状视图,并有我的所有目录和文件从一个路径

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


您已经使用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

请先搜索,然后再反复询问


你能帮我一个忙吗?你的TreeView控件叫什么名字?它对我有用。您将代码放在哪个事件处理程序中?在上面的示例中,它从按钮单击事件执行一次。您似乎已将代码放入其中一个TreeView单击/选择事件?在BeforeExpand TreeView事件上对路径“c:\Documents and Settings”的访问被拒绝。这里我没有这个名字的文件夹。。。
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
'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