Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


how to save ListBox in

xxn5xxn5 Member Posts: 5

how to save ListBox in
Dim files() As String
files = Directory.GetFiles("C:\", "*.mp3")

Dim file As String
For Each file In files

output :
00005.mp3 ....

want to save all item wth Only filenam to another folder.


  • CzareqPLCzareqPL PolandMember Posts: 2

    'You can do it like this:

     Dim YourFiles As New ArrayList
        Dim k As Integer
        For k = 0 To ListBox1.Items.Count - 1
        Next k
        'Now you can access it like this:
        For k = 0 To YourFiles.Count - 1

    'I prefer to use ArrayList instad of simple arrays because they are more flexible and you can inherit one arraylist inside another...

  • jdforsythejdforsythe OhioMember Posts: 2
    edited July 2014

    How exactly do you want to save the list? Just as a list in a text file? There are a bunch of ways.

    Dim inFiles() As String = Directory.GetFiles("C:\", "*.*")
    For i As Integer = 0 To inFiles.Length - 1
        ' replace the full path in the array with just the filename
        inFiles(i) = Path.GetFileName(inFiles(i))
    File.WriteAllLines("c:\list.txt", inFiles)

    Personally I'd probably use a List(Of String) instead of an array of string. It makes the For loop easier, but you have to convert the list to an array to WriteAllLines. Still, I like Lists...

    Dim inFiles As List(Of String) = Directory.GetFiles("C:\", "*.*").ToList
    For Each file As String In inFiles
        ' replace the entry in the list with just the filename
        file = Path.GetFileName(file)
    File.WriteAllLines("C:\list.txt", inFiles.ToArray)

    But the first option is likely the best option, since you don't have to convert to a list and then back to an array. It's unnecessary overhead.

    You can also use File.WriteAllText() to write a string to the file.

    Dim inFiles() As String = Directory.GetFiles("C:\", "*.*")
    For i As Integer = 0 To inFiles.Length - 1
        inFiles(i) = Path.GetFileName(inFiles(i))
    File.WriteAllText("c:\list.txt", String.Join(vbCrLf, inFiles))

    Note that you could also write this out using many other methods, including StreamWriter

Sign In or Register to comment.