This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

7 Sept 2013

VB.NET Program to get File Names in a given Directory

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

End Module

Sample Output : 
  

No comments:

Post a Comment

Thanks for your comments.
-Sameer