VB.NET Program to get File Names in a Folder / Directory
In this post we will see a VB.Net Program to retrieve list of file names in a given folder/directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Imports System.IO Module Module1 Sub Main() Try Dim FolderPath As String Dim Files As String() 'declaring string array Console.WriteLine("www.ProgrammingPosts.blogspot.com") 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 Try fi = New System.IO.FileInfo(FileName) Catch ex As System.IO.FileNotFoundException ' To inform the user and continue Console.WriteLine(ex.Message) Continue For End Try ' Console.WriteLine("{0}", fi.Directory); //prints filepath ' Console.WriteLine("{0}", fi.DirectoryName); // prints directorypath Console.WriteLine("{0}", fi.Name) Next Console.ReadLine() Catch ex As DirectoryNotFoundException Console.BackgroundColor = ConsoleColor.Red Console.WriteLine(vbLf & vbLf & " Directory Not Found..Press any key to exit...") Console.ReadKey() End Try End Sub |
Sample Output :