Get Sub Directories in VB.NET

In this article I describe how to get and set the sub directory list in VB.Net.
  • 4167

Get Sub Directories

The GetDirectories method of the Directory class loads all the subdirectories of a directory. To get all subdirectories, we can read subdirectories recursively.

Imports System.IO
Module
Module1
    Dim root As String = "C:\Temp"
    Public Sub GetSubDirectories()
        ' Get all subdirectories
        Dim subdirectoryEntries As String() = Directory.GetDirectories(root)
        ' Loop through them to see if they have any other subdirectories
        For Each subdirectory As String In subdirectoryEntries
            LoadSubDirs(subdirectory)
        Next
    End Sub
    Private Sub LoadSubDirs(dir As String)
        Console.WriteLine(dir)
        Dim subdirectoryEntries As String() = Directory.GetDirectories(dir)
        For Each subdirectory As String In subdirectoryEntries
            LoadSubDirs(subdirectory)
        Next
    End Sub
    Sub Main()
        GetSubDirectories()
        Console.ReadLine()
    End
Sub

End
Module

Output:

 subdirectories list.jpg

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.