如何获得特定路径目录中的所有文件夹、子文件夹和文件?
示例:
+ folder1
- exe1
+ folder2
- exe1
- exe2
+ folder3
- exe1
+ folder2
- exe1
+ folder3
+ folder4我现在正在使用:
Sub GetDirectories(ByVal StartPath As String)
For Each Dir As String In IO.Directory.GetDirectories(StartPath)
ListBox1.Items.Add(Dir)
ListBox1.Items.AddRange(IO.Directory.GetFiles(StartPath))
ListBox1.Items.AddRange(IO.Directory.GetFiles(Dir))
Next
End Sub以及:
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
For Each Dir As String In IO.Directory.GetDirectories(path)
GetDirectories(path)
Next
Next但它并没有给我其他子文件夹的所有文件。
编辑:使用with列表框,我希望在放入文件夹时看到完整的路径,然后给出所有的子文件夹和文件,
发布于 2020-04-28 19:04:34
您可以更好地使用TreeView来获得更方便的结果。
只需从TreeView中获取一个ToolBox,并使用以下代码列出特定目录和子目录(包括文件):
Private Enum ItemType
Drive
Folder
File
End Enum
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim node As TreeNode =
TreeView1.Nodes.Add("Hello") ' Specifying Folder Names
node.Tag = ItemType.Folder
node.Nodes.Add("FILLER")
End Sub
Private Sub file_view_tree_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
Dim currentNode As TreeNode = e.Node
currentNode.Nodes.Clear()
Try
'Now go get all the files and folders
Dim fullPathString As String = currentNode.FullPath
'Handle each folder
For Each folderString As String In
Directory.GetDirectories(fullPathString)
Dim newNode As TreeNode =
currentNode.Nodes.Add(Path.GetFileName(folderString))
Dim x As String = Path.GetFileName("")
newNode.Tag = ItemType.File
newNode.Nodes.Add("FILLER")
Next
'Handle each file
For Each fileString As String In
Directory.GetFiles(fullPathString)
'Get just the file name portion (without the path) :
Dim newNode As TreeNode =
currentNode.Nodes.Add(Path.GetFileName(fileString))
newNode.Tag = ItemType.File
Next
Catch ex As Exception
End Try
End Sub注意:我修改了堆栈溢出的this线程,并根据您的需求进行了定制。

https://stackoverflow.com/questions/61487110
复制相似问题