Trying to get all the files paths in a selected folder...

Archolm

Member
Joined
Feb 9, 2011
Messages
6
Programming Experience
Beginner
and then I want to display them in a listbox. It's for a little amateur project so i'm still very much a newbie when it comes to .net. I have gobbled together this code:
VB.NET:
  Dim theFolderBrowser As New FolderBrowserDialog
         theFolderBrowser.Description = "Please select a folder for the download."
         'theFolderBrowser.ShowNewFolderButton = False
         'theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop
         'theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.Desktop
         If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
             Label1.Text = theFolderBrowser.SelectedPath
             Dim di As New DirectoryInfo(Label1.Text)
             Dim fi As FileInfo() = di.GetFiles()
             Dim fiTemp As FileInfo
             For Each fiTemp In fi
                 ListBox1.Text = (fiTemp.Name)
             Next
         End If
I was hoping to get the actual path and then used that to get a bunch of mp3 files paths from a folder. However the above code gives me nothing in return. Could anyone give me a clue as to why it doesn't?
 
Last edited by a moderator:
There's no need to use a loop to add the items and, in fact, not doing so provides a significant advantage in this case. Just bind the FileInfo array to the ListBox:
VB.NET:
With Me.ListBox1
    .DisplayMember = "Name"
    .ValueMember = "FullName"
    .DataSource = New DirectoryInfo(theFolderBrowser.SelectedPath).GetFiles()
End With
Now, the ListBox will display just the file names and, when the user selects a file name, you can get the full path from the SelectedValue property of the ListBox.
 
Back
Top