使用 ProgressBar 复制目录和文件 [英] Copy directories and files with ProgressBar
问题描述
正在尝试创建一个控制台应用程序以将目录从源复制到目标,并且在复制文件时进度条不执行任何操作...
My.Computer.FileSystem.CopyDirectory(source, destination)对于 i = 1 到 100Console.Write(String.Format("复制进度:{0}%& vbCr, i))Threading.Thread.Sleep(100)下一个
或 ProgressBar 显示Copy Progress 1%";它一直在复制...
对于 i = 1 到 100Console.Write(String.Format("复制进度:{0}%& vbCr, i))My.Computer.FileSystem.CopyDirectory(源,目标)Threading.Thread.Sleep(100)下一个
想知道我做错了什么,因为我显然将 My.Computer 行放在了错误的位置!
一个简单的解决方案,使用 Linq Select
复制
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()私有函数 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)对于每个 sPath 作为 sourcePath 中的字符串Dim fileInfo As New DirectoryInfo(sPath).GetFiles()Dim numberOfFiles As Integer = fileInfo.Length - 1Dim progressBarPass As Double = (30/numberOfFiles)Dim 增量为 Double = 100/numberOfFilesDirectory.CreateDirectory(destPath)Console.CursorLeft = 0Console.Write("复制进度:")Console.CursorLeft = 20Console.Write(progressBarEmptySymbol)allFilesCopied.AddRange(fileInfo.选择(函数(f,我)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()下一个返回所有已复制的文件结束函数
有关使用更快的文件枚举器执行此任务的有趣方法,请参阅此 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 ProgressBar 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 ProgressBar 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 interesting method to perform this task with a much faster file enumerator, see this CodeProject article: A Faster Directory Enumerator
这篇关于使用 ProgressBar 复制目录和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!