VB 2013批量压缩列表框选定项目

时间:2014-07-05 18:43:51

标签: vb.net foreach listbox visual-studio-2013 zip

在这里寻找更多好建议。今天我写了一些代码,它采用了一个列表框选择项,并使用该选项压缩相应的文件(重命名日期和时间)并放在一个文件夹中,然后复制到另一个文件夹。现在我需要能够通过一个包含许多选项的列表框进行迭代,以便我可以批量压缩,重命名和复制。以下是我的尝试:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    'Button click events.
    'Start backup.

    Dim PjtPath As String = TextBox1.Text
    Dim ZipLocal As String = TextBox2.Text
    Dim ZipNetwk As String = TextBox3.Text

    Static Dim StartPath As String
    Static Dim ZipPath As String

    For Each a As String In ListBox1.SelectedItems()
        Dim PjtName As String = ListBox1.SelectedItems(a).ToString
        Dim ZipExt As String = Format(Now, " yyyy-MM-dd @ HHmm") & ".zip"

        If TextBox2.Text = String.Empty Then
            StartPath = PjtPath & "\" & PjtName
            ZipPath = PjtPath & "\" & PjtName & ZipExt
        ElseIf TextBox2.Text <> String.Empty Then
            StartPath = PjtPath & "\" & PjtName
            ZipPath = ZipLocal & "\" & PjtName & ZipExt
        End If
        System.IO.Compression.ZipFile.CreateFromDirectory _
        (StartPath, ZipPath, IO.Compression.CompressionLevel.Optimal, True)

        If TextBox3.Text <> String.Empty Then
            Dim ZipCopy As String = ZipNetwk & "\" & PjtName & ZipExt
            My.Computer.FileSystem.CopyFile(ZipPath, ZipCopy)
        End If
    Next
End Sub

我得到的错误是从字符串转换为整数无效。有人可以指出我正确的方向。非常感谢!!!

1 个答案:

答案 0 :(得分:1)

Dim PjtName As String = ListBox1.SelectedItems(a).ToString

改为......

Dim PjtName As String = a

傻傻的我!