vb.net 获取目录中的所有文件夹和子文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21595143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get all the folders and sub-folders in side a directory
提问by TestUser1
I am learning VB.net, I would like to know how to get all the folders and sub-folder s inside a directory and how to add them all to a listbox. I would also like it to list the folders while it is scanning like showing the current folders found. I have tried a few things but they never seem to work. I have tried this:
我正在学习 VB.net,我想知道如何获取目录中的所有文件夹和子文件夹,以及如何将它们全部添加到列表框。我还希望它在扫描时列出文件夹,例如显示找到的当前文件夹。我尝试了一些东西,但它们似乎从未奏效。我试过这个:
Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
Dim Dirs() As String = Directory.GetDirectories(StartPath)
DirectoryList.AddRange(Dirs)
For Each Dir As String In Dirs
GetDirectories(Dir, DirectoryList)
Next
For Each item In DirectoryList
ListBox1.Items.Add(item)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim DirList As New ArrayList
GetDirectories("c:\hexing\", DirList)
End Sub
回答by Vignesh Kumar A
Try this
尝试这个
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim DirList As New ArrayList
Dim Dirs() As String = Directory.GetDirectories(StartPath)
DirList.AddRange(Dirs)
For Each Dir As String In Dirs
GetDirectories(Dir, DirectoryList)
Next
Catch ex As Exception
End Try
End Sub
(Or)
(或者)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Dir As String In Directory.GetDirectories("c:\Program Files")
ListBox1.Items.Add(Dir)
Next
End Sub
Edit
编辑
According to VB.NET 05, List Folder, SubFolders, and Sub SubFolders:
The most efficient way would be to use recursivity:
最有效的方法是使用递归:
Private Function getAllFolders(ByVal directory As String) As String()
'Create object
Dim fi As New IO.DirectoryInfo(directory)
'Array to store paths
Dim path() As String = {}
'Loop through subfolders
For Each subfolder As IO.DirectoryInfo In fi.GetDirectories()
'Add this folders name
Array.Resize(path, path.Length + 1)
path(path.Length - 1) = subfolder.FullName
'Recall function with each subdirectory
For Each s As String In getAllFolders(subfolder.FullName)
Array.Resize(path, path.Length + 1)
path(path.Length - 1) = s
Next
Next
Return path
End Function
回答by MusicLovingIndianGirl
use the Directory.GetDirectoriesmethod.
DirectoryInfo dinfo = new DirectoryInfo("path");
dinfo.GetDirectories();
回答by user10795772
Simplest is:
最简单的是:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
GetDirectories(Label1.Text)
End Sub
Sub GetDirectories(ByVal StartPath As String)
For Each Dir As String In IO.Directory.GetDirectories(StartPath)
CheckedListBox1.Items.Add(Dir)
GetDirectories(Dir)
Next
End Sub