Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As of right now, I can get the folders to show up in treeview as the parent nodes, but the associated files that I wanted as child nodes are not showing up.

I am not sure what I am doing wrong, here is my code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim dir = "C:\Users\jj\Documents\Visual Studio 2017\Projects\windows media\windows media\videos"



       Dim I As Integer
       I += 1

           Dim dir1 = "C:\Users\jj\Documents\Visual Studio 2017\Projects\windows media\windows media\videos\"

       ' once inside your directory then filter the type of items you want to show, if you want all files just remove the text
       'filter and leave the dir.
       For Each direct As String In System.IO.Directory.GetDirectories(dir)

           'this line of code adds the filtered folders to a treeview control
           TreeView1.Nodes.Add(System.IO.Path.GetFileNameWithoutExtension(direct))

       Next

       For Each file As String In System.IO.Directory.GetFiles(dir1, "*.mp4")
           TreeView1.Nodes(I).Nodes.Add(System.IO.Path.GetFileNameWithoutExtension(file))
       Next



   End Sub


As you can see I used both getdirectories and getfiles. I tried to use a loop with an integer to go through each folder, problem is it's only showing the folders and no files.

What I have tried:

I have tried to change the path and switch the code up some, but the result is still the same.
Posted
Updated 19-Apr-17 19:50pm

1 solution

I changed your code a little bit to make it testible for me :

VB
Dim dir1 = "C:\windows"

 ' once inside your directory then filter the type of items you want to show, if you want all files just remove the text
 'filter and leave the dir.
 For Each direct As String In System.IO.Directory.GetDirectories(dir1)
     Dim dir As String = System.IO.Path.GetFileNameWithoutExtension(direct)
     'this line of code adds the filtered folders to a treeview control
     TreeView1.Nodes.Add(dir, dir)
     Dim inx As Integer = TreeView1.Nodes.Count
     Try
         For Each file As String In System.IO.Directory.GetFiles(direct + "\", "*.*")
             TreeView1.Nodes(inx - 1).Nodes.Add(System.IO.Path.GetFileName(file))
         Next
     Catch ex As Exception
     End Try
  Next


But I think this you can see what must be changed to your code ...
 
Share this answer
 
Comments
Member 11856456 20-Apr-17 11:23am    
I see there were only minor changes to make this code work appropriately. Thank you for your help.
Ralf Meier 21-Apr-17 7:09am    
You are welcome.
Thanks for your vote ... :)

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