使用进度条复制目录文件 [英] copy directory files with progress bar

查看:83
本文介绍了使用进度条复制目录文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建控制台应用程序以将目录从源复制到目标,并且在复制文件时进度条不执行任何操作...

  My.Computer.FileSystem.CopyDirectory(源,目标)对于i = 1到100Console.Write(String.Format(复制进度:{0}%"& vbCr,i))Threading.Thread.Sleep(100)下一个 

或进度条在整个复制过程中显示复制进度1%" ...

 对于i = 1到100Console.Write(String.Format(复制进度:{0}%"& vbCr,i))My.Computer.FileSystem.CopyDirectory(源,目标)Threading.Thread.Sleep(100)下一个 

想知道我在做什么错,因为我显然将My.Computer行放在错误的位置!

解决方案

一个简单的解决方案,使用Linq Select 复制

 将sourcePath设置为String()= New String(){"[SourcePath1]","[SourcePath2]","[SourcePath3]"}昏暗的destinationPath为String ="[DestinationPath]"昏暗的文件复制为列表(字符串)= CopyDirectoryWithProgress(sourcePath,destinationPath)Console.ReadLine()私有函数CopyDirectoryWithProgress(sourcePath as String(),destPath as String)as List(Of String)将昏暗的allFiles复制为列表(字符串)=新列表(字符串)Dim progressBarPassSymbol为Char = ChrW(& H25A0)Dim progressBarEmptySymbol As String = New String(ChrW(& H2014),30)对于每个sPath作为sourcePath中的字符串昏暗的fileInfo作为新的DirectoryInfo(sPath).GetFiles()昏暗的numberOfFiles作为整数= fileInfo.Length-1Dim progressBarPass为Double =(30/numberOfFiles)Double的Dim增量= 100/numberOfFilesDirectory.CreateDirectory(destPath)Console.CursorLeft = 0Console.Write(复制进度:")Console.CursorLeft = 20Console.Write(progressBarEmptySymbol)allFilesCopied.AddRange(fileInfo.选择(函数(f,i)File.Copy(Path.Combine(sPath,f.Name),Path.Combine(destPath,f.Name),True)Console.CursorLeft = 15Console.Write("{0:g}%"&新字符串(progressBarPassSymbol,CInt((i + 1)* progressBarPass)),CInt((i + 1)*增量))返回f.FullName结束功能))Console.WriteLine()下一个返回allFilesCopied结束功能 

有关使用更快的文件枚举器执行此任务的干预方法,请参见此CodeProject文章:更快的目录枚举器

Trying to create a console application to copy directories from the source to the destination and either the progress bar does nothing while files are copied ...

My.Computer.FileSystem.CopyDirectory(source, destination)

For i = 1 To 100
 Console.Write(String.Format("Copy progress: {0}%" & vbCr, i))
 Threading.Thread.Sleep(100)
Next

or the progress bar says "Copy Progress 1%" the entire time it's copying ...

For i = 1 To 100
 Console.Write(String.Format("Copy progress: {0}%" & vbCr, i))
 My.Computer.FileSystem.CopyDirectory(source, destination)
 Threading.Thread.Sleep(100)
Next

Wondering what I am doing wrong because I am obviously putting the My.Computer line in the wrong spot!

解决方案

A simple solution, using Linq Select to copy the file list returned by DirectoryInfo.GetFiles()

Pass the sample method an array of Source Directories and a Destination Directory.
The progress (0-100%) is printed to the Output window, and a progress bar gives a visual feedback of the copy status for each Source Path.

This method will return the list of all files copied.


Dim sourcePath As String() = New String() {"[SourcePath1]", "[SourcePath2]", "[SourcePath3]"}
Dim destinationPath As String = "[DestinationPath]"
Dim filesCopied As List(Of String) = CopyDirectoryWithProgress(sourcePath, destinationPath)
Console.ReadLine()

Private Function CopyDirectoryWithProgress(sourcePath As String(), destPath As String) As List(Of String)

    Dim allFilesCopied As List(Of String) = New List(Of String)
    Dim progressBarPassSymbol As Char = ChrW(&H25A0)
    Dim progressBarEmptySymbol As String = New String(ChrW(&H2014), 30)

    For Each sPath As String In sourcePath
        Dim fileInfo As New DirectoryInfo(sPath).GetFiles()
        Dim numberOfFiles As Integer = fileInfo.Length - 1
        Dim progressBarPass As Double = (30 / numberOfFiles)
        Dim increment As Double = 100 / numberOfFiles
        Directory.CreateDirectory(destPath)

        Console.CursorLeft = 0
        Console.Write("Copy progress: ")
        Console.CursorLeft = 20
        Console.Write(progressBarEmptySymbol)

        allFilesCopied.AddRange(fileInfo.
            Select(Function(f, i)
               File.Copy(Path.Combine(sPath, f.Name), Path.Combine(destPath, f.Name), True)
               Console.CursorLeft = 15
               Console.Write("{0:g}% " &
                   New String(progressBarPassSymbol, CInt((i + 1) * progressBarPass)),
                   CInt((i + 1) * increment))

               Return f.FullName
           End Function))
        Console.WriteLine()
    Next
    Return allFilesCopied
End Function

For an interensting method to perform this task with a much faster file enumerator, see this CodeProject article: A Faster Directory Enumerator

这篇关于使用进度条复制目录文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆