0

My directory structure is like below.

Parent directory
---Sub directory 1 ---Sub directory 2 ------Sub directory2a ------Sub directory2b ---Sub directory 3

I am writing in VB.net to fetch all the directory names (including sub directories in all the levels)

However while using directory.getfilesystementries(path) I am getting only the top level sub directories. Any idea on how to fetch all the subdirectory names of all sub levels?

Thomas Orlita
  • 1,554
  • 14
  • 28
Jeyaganesh
  • 1,334
  • 5
  • 26
  • 48

4 Answers4

4

just use something like this:

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

the trick is the SearchOption.AllDirectories

BTW: you can do the same with your GetFileSystemEntries-Method

Random Dev
  • 51,810
  • 9
  • 92
  • 119
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
LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
1

The Directoryinfo object can provide all sorts of information and about a directory, including directories / Files even system files

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
spacemonkeys
  • 1,689
  • 5
  • 16
  • 29
0

You need to use the overloaded version of Directory.GetFileSystemEntries that specifies whether or not to search subdirectories:

Dim allDirectories As String() = Directory.GetFileSystemEntries("path", "*", SearchOption.AllDirectories)

Directory.GetFileSystemEntries Method (String, String, SearchOption)

Tim
  • 28,212
  • 8
  • 63
  • 76