Enumerate Directory Using VB.NET

In this article I describe how to enumerate a given directory in VB.Net
  • 3670

Enumerate Directory in VB

 The Directory.EnumerateDirectories method returns an enumerable collection of directory names in the specified directory.

The following code snippet displays collection of directory names in the specified directory.

Imports System.IO

Module Module1

    Sub Main()

        Dim root As String = "C:\Temp"

        ' Get a list of all subdirectories

        Dim dirs = From folder In Directory.EnumerateDirectories(root)

        Console.WriteLine("Subdirectories: {0}", dirs.Count().ToString())

        For Each folder In dirs

            Console.WriteLine("{0}", folder.Substring(folder.LastIndexOf("\") + 1))

        Next

        ' Get a list of all subdirectories starting with 'Ma'

        Dim MaDirs = From folder In Directory.EnumerateDirectories(root, "Ma*")

        Console.WriteLine("Subdirectories: {0}", MaDirs.Count().ToString())

        Console.WriteLine("List of Subdirectories starts with Ma:")

        For Each folder In MaDirs

            Console.WriteLine("{0}", folder.Substring(folder.LastIndexOf("\") + 1))

        Next

 

        Console.ReadLine()

    End Sub

End Module

Output:

Enumerate directories in vb.jpg

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.