Get All Files under a Directory in VB.NET
Today in VB.NET, I will teach you how to create a program that gets all files under its corresponding directory not all the folders that are inside on it.
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.
2. Next, add one ListBox named ListBox1 for displaying the files, one textbox named textbox1 for inputting the directory, and one button named button1 to get all the files in the directory. You must design your interface like this:
3. Add this code as your button1. This will get all the files under the inputted directory.
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim arrDirs() As String 'declare an array for all the files
- Dim strDir As String 'declare string for directory
- 'get all the files inputted in the inputted directory of textbox1
- arrDirs = Directory.GetFiles(TextBox1.Text)
- 'create a for each loop to pass all the files into the string variable
- For Each strDir In arrDirs
- 'add the files to the lisbox
- Catch ex As Exception
- 'if directory doesnt exist, it will promt the user that the path doesnt exist
- End Try
- End Sub
We initialized variable arrDirs() as string to declare an array for all the files and variable strDir As String 'o declare string for directory. Next, we have created a try and catch method. In the try method, the arrDirs will hold all the files under the directory inputted in textbox1. And then we have created a for loop that that passes all the files into the string variable. Inside the loop we have added all the files into the listbox. And in Catch method, if directory inputteddoesnt exist, it will promt the user that the path doesnt exist.
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware