我試圖把我的文件複製表單上的進度,得到它的工作,但進度欄更新的基礎上的文件數量。問題是有很多小文件,還有一些非常大的文件需要複製。使用字節複製文件+進度條
所以我想知道如果不是使用文件編號,而是有辦法檢查正在寫入的字節數。 任何建議,非常感謝。下面是代碼我現在有:
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
string SourcePath = RegistryRead.ReadOriginalPath();
string DestinationPath = RegistryRead.ReadNewPath();
if (Directory.Exists(SourcePath))
{
//Now Create all of the directories
string[] allDirectories = Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories);
string[] allFiles = Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories);
int numberOfItems = allDirectories.Length + allFiles.Length;
int progress = 0;
foreach (string dirPath in allDirectories)
{
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
progress++;
BackgroundWorker.ReportProgress(100 * progress/numberOfItems);
}
//Copy all the files
foreach (string newPath in allFiles)
{
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
progress++;
BackgroundWorker.ReportProgress(100 * progress/numberOfItems);
}
}
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Change the value of the ProgressBar to the BackgroundWorker progress.
progressBar1.Value = e.ProgressPercentage;
}
在此先感謝
用戶做已經,但它是被證明麻煩的細節。 –
我可能會在這裏失明。它在哪裏說呢? – zmbq
用戶已經完成了您所描述的內容,但遇到了細節問題。對於不同大小的文件,進度條不會以統一的速率移動。因此需要在進度計算中考慮文件大小。 –