我正在尝试在我的文件复制表单上取得进展,让它工作但进度条根据文件数量更新。 问题是有很多小文件,还有一些非常大的文件需要复制。
所以我想知道是否有一种方法来检查写入的字节数而不是使用文件号。 任何建议都非常感谢。 这是我现在的代码:
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;
}
提前致谢