【问题标题】:Visual Basic Progressbar while Copy复制时的 Visual Basic 进度条
【发布时间】:2016-09-30 08:04:25
【问题描述】:

和标题一样,我想要一个进度条,它在复制时显示过程。

到目前为止我的代码:

导入 System.IO 导入脚本

公开课表1 暗淡的源,目的地为字符串 Dim fso As FileSystemObject = New FileSystemObject 将 SourceSize 调暗为 Double

Private Sub ResetThings()
    ProgressBar1.Value = 0

End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim xFilesCount = Directory.GetFiles(Source).Length
    Dim xFilesTransferred As Integer = 0

    For Each xFiles In Directory.GetFiles(Source)
        My.Computer.FileSystem.CopyDirectory(Source, Destination, True)
        xFilesTransferred += 1
        ProgressBar1.Value = xFilesTransferred * 100 / xFilesCount
        ProgressBar1.Update()
    Next
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs)

End Sub

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs)

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    FolderBrowserDialog1.ShowDialog()
    Source = FolderBrowserDialog1.SelectedPath
    first.Text = "Original: " & Source
    ResetThings()
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    FolderBrowserDialog1.ShowDialog()
    Destination = FolderBrowserDialog1.SelectedPath
    Dim Temp = Source
    Do
        Temp = Temp.Substring(1)
    Loop Until Temp.Contains("\") = False
    Destination = Destination
    Second.Text = "Ziel: " & Destination

End Sub

【问题讨论】:

  • 你有什么问题?
  • 进度条没有显示任何内容。但是文件正在复制...
  • 我建议通过查看它,您需要设置ProgressBar1.Maximum。然后你可以使用这一行ProgressBar1.Increment(1) 让它在每个文件之后上升一个。并且因为您设置了最大值,所以一旦完成所有文件,进度条应该是完整的
  • 好的,谢谢.. 但是如何在我的代码中使用 ProgressBar1.Maximum 和 ProgressBar1.Increment(1)?第一次使用进度条:)
  • 问题是你没有复制文件,而是整个目录——这是一个命令,不会刷新任何东西。

标签: vb.net


【解决方案1】:

您的主要问题是My.Computer.FileSystem.CopyDirectory(Source, Destination, True) 是一个同步调用,只要您将源目录的所有内容复制到目标目录,它就会阻塞 UI 并禁止任何交互。

你需要:

  1. 分解复制过程以复制单个文件而不是 整个目录
  2. 使复制过程异步以保持 UI 响应并允许 进度条待更新。

对于第一个目标,在循环中为目录中的每个文件使用File.Copy(soruceFile, destinationFile),例如:

For Each xFileWithPath In Directory.GetFiles(Source)
     Dim xFile As String = Path.GetFileName(xFileWithPath)
     Try
         File.Copy(xFileWithPath, Path.Combine(Destination, xFile))
         //...
     Catch ex As Exception
          //something went wrong
      End Try
 Next

第二个使用BackgroundWorker 并设置所需的属性+事件处理程序:

 Dim bw As New BackgroundWorker()
 bw.WorkerReportsProgress = True
 AddHandler bw.DoWork, AddressOf copyFiles
 AddHandler bw.ProgressChanged, AddressOf updateUI

DoWork 是 BackgroundWorker 执行的任务。 ProgressChanged 是您要订阅以更新 ProgressBar 的事件。

updateUI 可能看起来像:

Private Sub updateUI(sender As Object, e As ProgressChangedEventArgs)
        ProgressBar1.Value = e.ProgressPercentage
        ProgressBar1.Update()
End Sub

copyFiles结合了复制部分+进度更新部分:

 Private Sub copyFiles(sender As Object, e As DoWorkEventArgs)
        Dim xFilesCount = Directory.GetFiles(Source).Length
        For Each xFileWithPath In Directory.GetFiles(Source)
            Dim xFile As String = Path.GetFileName(xFileWithPath)
            Try
                File.Copy(xFileWithPath, Path.Combine(Destination, xFile))
                xFilesTransferred += 1
                bw.ReportProgress(xFilesTransferred * 100 / xFilesCount)
            Catch ex As Exception
                 //Something went wrong
            End Try
        Next
    End Sub

最终你必须在你的按钮点击事件中启动 BackgroundWorker:

 bw.RunWorkerAsync()

编辑: 如果你想复制所有子目录的所有文件,包括SearchOption.AllDirectories作为最后一个参数到Directory.GetFiles

【讨论】:

  • 非常感谢.. 但我对 vb 没有太多经验。你能把整个脚本像一个文件一样给我吗?
  • 没有。如果我为您编写代码,您将不会学到任何东西。特别是如果你没有太多经验。只需告诉我您不了解哪一部分或您遇到的困难,我很乐意帮助您朝着正确的方向前进。
【解决方案2】:

由于评论线程中的新信息,我更新了我的答案。

我已经对此进行了测试,它可以在我的计算机上运行。我现在已经更改了它,以便您读取目录中的文件总数作为最大值,现在您正在循环浏览源目录中的每个文件并复制文件并跟踪和进度条更新。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim xFilesCount = Directory.GetFiles(source).Length
    Dim xFilesTransferred As Integer = 0

    ProgressBar1.Maximum = xFilesCount

    For Each xFiles In Directory.GetFiles(source)
        Dim fileinfo As FileInfo = New FileInfo(xFiles)
        System.IO.File.Copy(xFiles, Path.Combine(Destination, fileinfo.Name), True)
        xFilesTransferred += 1
        ProgressBar1.Increment(1)
        ProgressBar1.Refresh()
    Next
End Sub

如果你想做更多的目录,你可以使用递归或循环遍历目录。我假设您正在创建一个备份程序,此链接可能有用:Using Recursion To Create A Program To Backup Folders

【讨论】:

  • 坦克,但进度条没有显示任何内容.. 也许我必须在进度条属性中编辑一些内容
  • 我一直在阅读,在您的进度栏属性中,将 STEP 属性设置为与您正在制作的 INCREMENT 相同的大小。来源:Source
  • Step 属性为 10
  • step 属性应该是 1,因为这就是你实现的程度
  • 还是同样的问题。
猜你喜欢
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多