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: design 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
  4.  
  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. Output: outputoutput 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: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment