简体   繁体   English

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

[英]Copying over both files and folders from a listbox vb.net

I already have the code that will copy over any files listed in a listbox but i just need help on how to adapt it to also copy over directories. 我已经有了将复制到列表框中列出的任何文件中的代码,但是我只需要有关如何使其适应目录复制的帮助。

Ex. 例如 of Listbox 列表框

  1. Z:\\Test\\TestFile.exe Z:\\ Test \\ TestFile.exe
  2. Z:\\Test\\TestFolder Z:\\ Test \\ TestFolder

This is the code i have so far... thank you in advance 这是我到目前为止的代码...提前谢谢

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

You have to use the same logic that you did with files, instead of copying files change it to copy directories. 您必须使用与文件相同的逻辑,而不是复制文件,而是将其更改为复制目录。 Below is the code: 下面是代码:

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

You have to assign the minimum and the maximum value for the progress bar control. 您必须为进度条控件分配最小值和最大值。 For instance: 0 as the minimum and the total number of items you have in your ListBox as the maximum. 例如:最小为0,列表框中的项目总数为最大。 In a loop the progress bar value is going to increase by one until it reaches its maximum. 在一个循环中,进度条值将增加一个,直到达到最大值。 Below is the code: 下面是代码:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM