Subdirectories Learn programming Visual Basic (VB.net)

Lesson:

Additional Libraries


Exercise:

Subdirectories 36


Objetive:

Create a program to store the files which are in a certain directory and its subdirectories.

Then, it will ask the user which text to search and it will display the files containing that text in their name.

Program will end when the user enters an empty search string.


Code:

Imports System
Imports System.IO

Class Subdirectories
    Private Shared Sub Main()
        Try
            Dim text As String = ""
            Console.Write("Enter a directory for search: ")
            text = Console.ReadLine()

            While text <> ""
                Dim directory As DirectoryInfo = New DirectoryInfo(text)
                Dim files As FileInfo() = directory.GetFiles("*.*")
                Dim directories As DirectoryInfo() = directory.GetDirectories()
                Dim i As Integer = 0

                While i < files.Length
                    Console.WriteLine((CType(files(i), FileInfo)).FullName)
                    i += 1
                End While

                For i = 0 To directories.Length - 1
                    Console.WriteLine((CType(directories(i), DirectoryInfo)).FullName)
                Next

                Console.Write(vbLf & "Enter a directory for search: ")
                text = Console.ReadLine()
            End While

        Catch ex As Exception
            Console.WriteLine(ex.ToString())
        End Try
    End Sub
End Class