Hey.
how can you load files into a list box.
Say i have a folder C:\Files and i want to add ONLY the .mp3 file names into a listbox how would i do this?
Thanks
Printable View
Hey.
how can you load files into a list box.
Say i have a folder C:\Files and i want to add ONLY the .mp3 file names into a listbox how would i do this?
Thanks
To get only .mp3 files change di.GetFiles() to di.GetFiles(.mp3).Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInfo("c:\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Sub
(I think)
Almost, it's
and to add to the listbox you probably only want the filename, so it would beCode:di.GetFiles("*.mp3")
Code:ListBox1.Items.Add(dra.Name)
Hey it works great thanks all~!
Or to be brief about it, and, lose the performance hit of a for-next, and, lose the resources caused by declaring the FileInfo and other class instance:
VB Code:
Dim di As New System.IO.DirectoryInfo("[b]C:\Documents and Settings\You\My Documents\My Music[/b]") ListBox1.Items.AddRange(di.GetFiles("*.mp3"))
those are the only two lines you need... just make sure to change the path highlighted in bold or you'll
get a path not found error.
Yeah that's a much better way to do it. :D