Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

winforms - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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: emp 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:	emp")
    ndParent.Tag = "c:	emp"
    '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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...