how to save ListBox in vb.net

Dim files() As String
files = Directory.GetFiles("C:\", "*.mp3")

Dim file As String
For Each file In files
    ListBox1.Items.Add(Path.GetFileName(file))
Next

output :
00001.mp3
00003.mp3
00005.mp3 ....

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

Recommended Answers

All 6 Replies

Something like this might help:

For Each file As String In Directory.GetFiles("C:\", "*.mp3")
    File.Copy(file, "MyNewDirectory\" & Path.GetFileName(file))
Next

If you need to do it with only the names in the listbox

For Each name As String In ListBox1.Items
    File.Copy("C:\" & name, "MyNewDirectory\" & name)
Next

If you want the original file deleted replace File.Copy with Directory.Move. the arguments stay the same.

For Each name As String In ListBox1.Items
File.Copy("C:\" & name, "MyNewDirectory\" & name)
Next

Could not find file 'C:\00003.mp3_[10_controls]_[customizable]'.

when folder name added
C:\00003.mp3' already exists.

file in ListBox1 are located in diff folder and sub folder.

Make sure you have the proper order of the folder names. In my example 'C:\' is the source folder and '"MyNewDirectory\' is the destination folder.

it does find nor save file from sub folder

For Each name As String In ListBox1.Items
File.Copy("C:\" & name, "MyNewDirectory\" & name)
Next

it does save file from sub folder.
as ListBox1 has only filename.

One thing you can do is switch to a ListView with the view set to 'List'. It will look just like a listbox, but the advantage is that each item has a subitem collection property where you can add the folder and/or the complete path as a subitem. Only the filename will show but you can access the subitem with code to get the source folder for the file.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListBox1.Text = My.Settings.history
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    My.Settings.history = ListBox1.Text
    My.Settings.Save()
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.