Imports System.IO Public Class Form1 Dim CurrentDirectory As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.CenterToParent() TreeView1.Nodes.Add("C:") PopulateTreeView("C:\", TreeView1.Nodes(0)) TreeView1.Nodes(0).Tag = "Done" End Sub Private Sub TreeView1_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick If e.Node.Tag <> "Done" then PopulateTreeView(e.Node.Text, e.Node) e.Node.Tag = "Done" End If End Sub Public Sub PopulateTreeView(ByVal directoryValue As String, ByVal parentNode As TreeNode) Dim DirectoryArray As String() If directoryValue = "Access Denied" Then Exit Sub End If Try DirectoryArray = Directory.GetDirectories(directoryValue) Catch ex As UnauthorizedAccessException Dim myNode As TreeNode = New TreeNode("Access Denied") parentNode.Nodes.Add(myNode) Exit Sub End Try If DirectoryArray.Length <> 0 Then For Each CurrentDirectory In DirectoryArray If Microsoft.VisualBasic.Mid(CurrentDirectory, 4, 25) <> "System Volume Information" Then Dim myNode As TreeNode = New TreeNode(CurrentDirectory) parentNode.Nodes.Add(myNode) End If Next End If End Sub End Class
<pre>
Dim myNode As TreeNode = New TreeNode(CurrentDirectory)
Dim myNode As TreeNode = New TreeNode(CurrentDirectory.Substring(CurrentDirectory.LastIndexOf("\") + 1))
to maintain spacing in a question. Also like seeing the use of substring(xxx.lastindexof("\") + 1). Your solution has a problem though... The CurrentDirectory variable is parentNode for the next drilldown (on that node), and so must be the entire path from the drive to the last subdirectory. For example, I was running on F:\today\ and today contains a subdirectory named 'hold.' when I tried to drill down on today, I got the below error System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'D:\DriveD\Visual Studio\Projects\Visual Basic\aTreeViewExample\bin\Debug\Today'.' I've been otherwise occupied for a day or so, I will keep trying to solve... Thanks
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)