Click here to Skip to main content
15,922,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code that will populate a treeview with a given drive's directories. But each node (subdirectory) shows the entire path of the subdirectory. I would like to see

instead of

Here is my code:
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
        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

            DirectoryArray = Directory.GetDirectories(directoryValue)
        Catch ex As UnauthorizedAccessException
            Dim myNode As TreeNode = New TreeNode("Access Denied")
            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)
                End If
        End If

    End Sub

End Class

May not be easily done, but I'm just learning VB.Net so I have no idea. Any
help would be greatly appreciated.

Also, the CodeProject preview (before posting question) shows every line of my question starting in column 1. Don't know how it will show up when posted into CodeProject...


What I have tried:

A bunch of google searches but gave up on that...
Updated 15-Jul-20 7:27am
phil.o 20-Nov-19 5:08am    
You saw every line starting at column 1 because you did not include your code block in proper <pre> tag. I corrected the formatting of your question, you can watch for what the raw source should look like.

Try to replace
Dim myNode As TreeNode = New TreeNode(CurrentDirectory)
Dim myNode As TreeNode =
New TreeNode(CurrentDirectory.Substring(CurrentDirectory.LastIndexOf("\") + 1))
Share this answer
Member 14633063 21-Nov-19 20:47pm    
Thanks for the tip on how to use
 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...

phil.o 22-Nov-19 4:54am    
You will have to debug to see exactly what is going on. Do you need help on debugging?
Also, please only use <pre> tags for code blocks, don't use them for normal text, and avoid them in comments.
Member 14633063 21-Nov-19 20:50pm    
Or maybe I'm confused as to exactly what the problem is...
Anyway, will keep looking thanks
No solution, just changing to 'closed.'
Share this answer
CHill60 16-Jul-20 4:24am    
There is no such status. I advise you to not post meaningless solutions to your own questions and then accept them. It looks like rep-point hunting and many trigger-happy members will be more than happy to report you for site abuse.
As soon as a post has a solution posted it drops out of the "Unanswered Questions" list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900