I’m trying to put a progress on my file copy form, got that to work but the progress bar updates based on the number of files. Problem is that there are a lot of small files, and some really big ones that need to be copied.
So I’d like to know if instead of using the file number, if there’s a way to check the number of bytes being written instead.
Any suggestions are much appreciated. Here is the code I have now:
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;
}
Thanks in advance
If you want to do this yourself you can
There is quite possibly a .net managed equivalent to CopyFileEx.
However, I would recommend calling SHFileOperation and let the system do the hard work for you. As you are discovering this task is exceedingly hard to do well. As an added benefit of using the shell to do the work, you’ll get the standard system dialog.