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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…