Get all folders and subfolders in the directory side

I am studying VB.net, I would like to know how to get all the folders and subfolders s inside the directory and how to add them all to the list. I would also like it to display folders during scanning, as shown by the current found folders. I tried a few things, but they never work. I tried this:

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
+3
source share
4 answers

Try it

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, the list of folders, subfolders and subfolders :

The most efficient way would be to use recursion:

 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
+7
source

use the Directory.GetDirectories method .

DirectoryInfo dinfo = new DirectoryInfo("path");

dinfo.GetDirectories();
+3

:

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
+1

, , , , ,

:

  • C:\Windows\< , .
  • C:\Windows\System32 <
  • C: \ Windows \ System32 \ Drivers \ <I do not need a list of files in this folder or any other folder inside System32
  • C: \ Windows \ Boot <I want to get a list of all the files in this folder
  • C: \ Windows \ Boot \ BCD <I do not want the list of files in this folder

Do you understand what I mean?

0
source

All Articles