2011-08-29 57 views
0

我的目錄結構如下所示。如何使用vb.net獲取指定路徑中的所有目錄和子目錄的列表

父目錄
--- Sub目錄1 --- Sub目錄2 ------子directory2a ------子directory2b --- Sub目錄3

我正在寫在VB.net獲取所有的目錄名稱(包括所有級別的子目錄)

但是,雖然使用directory.getfilesystementries(路徑)我只得到頂級子目錄。有關如何獲取所有子級別的所有子目錄名稱的想法?

回答

1

只是用這樣的:

Dim result = System.IO.Directory.EnumerateDirectories(path, "*", System.IO.SearchOption.AllDirectories) 

的伎倆是SearchOption.AllDirectories

BTW:你可以做同樣的你的GetFileSystemEntries法

+0

+1打我2分鐘後,用不同的方式做到這一點,但相同的關鍵部分(SearchOption.AllDirectories :) :) – Tim

+0

謝謝 - 一直髮生在我身上;) – Carsten

+0

只要OP得到他們需要的答案,這一切都很好。 – Tim

0

的DirectoryInfo對象可以提供各種信息和目錄,包括目錄/文件甚至系統文件

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    Dim dir As New DirectoryInfo("C:\") 
    For Each dirItem As DirectoryInfo In dir.GetDirectories 
     MsgBox(dirItem.Name) 
    Next 
End Sub 
1
Dim di As New DirectoryInfo(FolderName) 
di = New DirectoryInfo(path) 

rgFiles = di.GetFiles("*.*", IO.SearchOption.AllDirectories) 

For Each fi As FileInfo In rgFiles 
    If CheckIfExist(fi.FullName.ToString.Replace("\" & fi.Name, "")) = False Then 
     ListBox1.Items.Add(fi.FullName.ToString.Replace("\" & fi.Name, "")) 
    End If 
Next 

Public Function CheckIfExist(ByRef Path As String) As Boolean 
    Dim RetVal As Boolean = False 

    For Each LI As String In ListBox1.Items 
     If LI.ToString = Path Then 
      RetVal = True 
      Return RetVal 
      Exit Function 
     End If 
    Next 
    Return RetVal 
End Function 
相關問題