In this post we will see a VB.Net Program to retrieve list of file names in a given folder/directory.


Imports System.IO
Module Module1

Sub Main()
Dim FolderPath As String
Dim Files As String() 'declaring string array
            Console.WriteLine(vbLf & ">>>VB.NET PROGRAM To PRINT FILES NAMES IN A Given Directory  <<<")
            Console.WriteLine(" Enter the Directory Path to get File Names in it : ")
            FolderPath = Console.ReadLine()
            Files = Directory.GetFiles(FolderPath)
            Console.WriteLine(vbLf & " The FileNames with extension in given Directory are : " & vbLf & vbLf)

For Each FileName As String In Files
' Creating the FileInfo object
Dim fi As System.IO.FileInfo = Nothing
                    fi = New System.IO.FileInfo(FileName)
Catch ex As System.IO.FileNotFoundException
' To inform the user and continue
                    Continue For
End Try
' Console.WriteLine("{0}", fi.Directory);  //prints filepath
' Console.WriteLine("{0}", fi.DirectoryName); // prints directorypath
                Console.WriteLine("{0}", fi.Name)
Catch ex As DirectoryNotFoundException
            Console.BackgroundColor = ConsoleColor.Red
            Console.WriteLine(vbLf & vbLf & " Directory Not Found..Press any key to exit...")
End Try
End Sub

Sample Output : 

VB.Net FileNames in Directory

Leave a Reply

Your email address will not be published.