This project is read-only.

Compress multi files selected from a multiselect listbox?

May 29, 2010 at 2:57 AM
Edited May 30, 2010 at 3:44 AM

Hi all, im trying to get this to work but having no luck, hopefully somebody can help.


First of im in 2008, i have a form with 2 listboxes, list box 1 displays the subfolders within a selected folder and listbox 2 displays the files inside the folder highlighted in listbox 1. The code for doing this is as follows:

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim lb1s As String = FolderBrowserDialog2.SelectedPath
Dim di As New IO.DirectoryInfo(lb1s)
Dim mydirInfoArray As IO.DirectoryInfo() = di.GetDirectories
'list the names of all files in the specified directory
ListBox1.ValueMember = "FullName"
ListBox1.DataSource = mydirInfoArray
ListBox1.DisplayMember = "Name"
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim lb1s2 As String = ListBox1.SelectedValue
Dim di As New IO.DirectoryInfo(lb1s2)
Dim myFileInfoArray As IO.FileInfo() = di.GetFiles()
'list the names of all files in the specified directory
ListBox2.ValueMember = "FullName"
ListBox2.DataSource = myFileInfoArray
ListBox2.DisplayMember = "Name"
End Sub

Now what im trying to do is compress the files selected in listbox 2, i can do it if only 1 file is selected, but i want to compress all selected files, into 1 archive, so i have a button with this code on it:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim zdllov As String = Application.StartupPath() & "\7z\" & "7z.dll"

Dim cmp As SevenZipCompressor = New SevenZipCompressor()

cmp.ArchiveFormat = OutArchiveFormat.SevenZip
cmp.CompressionMethod = SevenZip.CompressionMethod.Default
cmp.CompressionMode = SevenZip.CompressionMode.Create
cmp.CompressionLevel = CompressionLevel.Ultra

Dim tocomp As String = ListBox2.SelectedValue

cmp.CompressFiles("C:\0000\listbox 2 files\Default.7Z", tocomp) 'cmp.CompressFiles(OutFile, Infile)
End Sub


This code works as i said but only for single files, if i select more then one file it does nothing, been new to coding (started learning in febuary :) ) im now stuck, is there a way to do this?

I've tried changing the tocomp string to listbox2.selecteditems but thats not supported.

Any helps much appreciated, Thanks.

Best regards, Dragon.


ps. If im attempting the impossible here, is it possible to add int he ability to use a filearray as the file to compress (i could add listbox items to file array, filearray to compressor tha way)

Jun 1, 2010 at 10:12 AM
Edited Jun 1, 2010 at 10:15 AM

Hello there, 

   You are not passing a string array. You are just passing just one selected item string value.

It should be something like this:


Dim tocomp(ListBox2.SelectedItems.Count) as String  'Creates an array the same size as the count of selected items.

ListBox2.SelectedItems.CopyTo(tocomp,0)  'Copies selected item content into the array

cmp.CompressFiles("C:\0000\listbox 2 files\Default.7Z", tocomp)


I am a C# person coming from VB6 background, however I think my solution should work in VB.NET you might just need to change it slightly.

I think there aren't any errors, its been so long since I typed VB code.