Last time, while I was working on the entertainment feature of the new version of Desktop Locker, I needed to automatically get all the music files in its Resources folder. That led me to work out the code below, which collects the file paths of a specific format in a folder into an ArrayList. For example, if you want all the mp3 files in a music folder, you can use this code to do it.

Get file paths of a specific format in a folder and store them in an ArrayList

Interface:

Code:

Public Class Form1
    Dim file As New ArrayList

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each filetmp As String In My.Computer.FileSystem.GetFiles(TextBox1.Text, FileIO.SearchOption.SearchTopLevelOnly, "*." & TextBox2.Text)

            file.Add(filetmp)

        Next

        For y = 0 To file.Count - 1
            ListBox1.Items.Add(file(y))
        Next

    End Sub
End Class

Code explanation:

My.Computer.FileSystem.GetFiles is used with the *.* pattern to search the specified folder for all files. The FileIO.SearchOption.SearchTopLevelOnly parameter means that only the top level of the folder is searched, so the second and third levels of subfolders are not searched. Each file that matches *.* has its path added to the file ArrayList. Finally, a loop outputs the data in file to ListBox1.

Project file download: Xun6 | 4Shared | Box.net