Get All Files under a Directory in VB.NET

Submitted by: 
Visitors have accessed this post 556 times.

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.

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim arrDirs() As String 'declare an array for all the files
  3. Dim strDir As String 'declare string for directory
  5. Try
  6. 'get all the files inputted in the inputted directory of textbox1
  7. arrDirs = Directory.GetFiles(TextBox1.Text)
  8. 'create a for each loop to pass all the files into the string variable
  9. For Each strDir In arrDirs
  10. 'add the files to the lisbox
  11. ListBox1.Items.Add(strDir)
  12. Next
  13. Catch ex As Exception
  14. 'if directory doesnt exist, it will promt the user that the path doesnt exist
  15. MessageBox.Show(ex.Message)
  16. End Try
  17. 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.

Best Regards,

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.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook:

Visit and like my page on Facebook at:

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.