通过特定目录来显示与treeview中的这些文件夹关联的文件夹和文件 [英] Going though a particular directory to show folders and files associated with those folders in treeview
问题描述
截至目前,我可以将文件夹显示在树视图中作为父节点,但我想要作为子节点的相关文件未显示。
我不确定我做错了什么,这是我的代码:
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
如你所见,我使用了getdirectories和getfiles。我试图使用一个带整数的循环遍历每个文件夹,问题是它只显示文件夹而没有文件。
我尝试过:
我试图更改路径并将代码切换一些,但结果仍然相同。
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.
推荐答案
我改变了你的代码以使其适合我:
I changed your code a little bit to make it testible for me :
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 ...
这篇关于通过特定目录来显示与treeview中的这些文件夹关联的文件夹和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!