从列表框vb.net复制文件和文件夹

时间:2016-01-22 00:17:45

标签: vb.net listbox

我已经有了复制列表框中列出的任何文件的代码,但我只需要帮助它如何调整它以复制目录。

实施例。列表框

  1. Z:\测试\ TestFile.exe
  2. Z:\测试\ TestFolder
  3. 这是我到目前为止的代码......提前谢谢

    For Each item As String In FilesList.Items
        Try
            If IO.File.Exists(item) Then
            My.Computer.FileSystem.CopyFile(item, 
                     FolderChosenText.Text & "\" & IO.Path.GetFileName(item))
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    Next
    

2 个答案:

答案 0 :(得分:1)

您必须使用与文件相同的逻辑,而不是复制文件将其更改为复制目录。以下是代码:

            If IO.Directory.Exists(item) Then
                My.Computer.FileSystem.CopyDirectory(item,
                FolderChosenText.Text & "\" & IO.Path.GetFileName(item))
            End If

答案 1 :(得分:1)

您必须为进度条控件指定最小值和最大值。例如:0作为最小值,ListBox中的项目总数为最大值。在循环中,进度条值将增加1,直到达到其最大值。以下是代码:

            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = FilesList.Items.Count - 1

            For i = 0 To FilesList.Items.Count - 1
                ProgressBar1.Value = i
            Next