Results 1 to 6 of 6

Thread: Load Files into listbox

  1. #1

    Thread Starter
    Addicted Member Stick's Avatar
    Join Date
    Aug 1999
    Location
    Iowa
    Posts
    152

    Load Files into listbox

    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

  2. #2
    Member JesusFreak's Avatar
    Join Date
    Feb 2003
    Location
    Headed for Heaven
    Posts
    57
    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
    To get only .mp3 files change di.GetFiles() to di.GetFiles(.mp3).
    (I think)
    That if you confess with your mouth, "Jesus is Lord," and believe in your heart that God raised him from the dead, you will be saved.

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Almost, it's
    Code:
    di.GetFiles("*.mp3")
    and to add to the listbox you probably only want the filename, so it would be
    Code:
    ListBox1.Items.Add(dra.Name)

  4. #4

    Thread Starter
    Addicted Member Stick's Avatar
    Join Date
    Aug 1999
    Location
    Iowa
    Posts
    152

    thx alot..

    Hey it works great thanks all~!

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. Dim di As New System.IO.DirectoryInfo("[b]C:\Documents and Settings\You\My Documents\My Music[/b]")
    2.  
    3. 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.
    Last edited by nemaroller; Apr 17th, 2003 at 04:48 PM.

  6. #6
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Yeah that's a much better way to do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width