private async void btnCopy_Click(object sender, EventArgs e) { srcPath = txtSrcInput.Text.TrimEnd(new[] { '\\', '/' }); dstPath = txtDstInput.Text.TrimEnd(new[] { '\\', '/' }); // The source path must be vaild and exist. The source and destination paths cannot // be the same. if (checkPathErrors()) { return; } // If destination directory does not exist ask to create it if (!Directory.Exists(dstPath)) { DialogResult dialogResult = MessageBox.Show( String.Format("Destination directory {0} doesn\'t exist\nCreate it?", dstPath), "Warning!", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.No) { return; } // Create the directory try { Directory.CreateDirectory(dstPath); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); return; } } totalDirs = 0; fileCount = 0; totalDirs = totalFiles = 0; DirOps.DirInfo info; DirOps.Options options = GetOptions(); info = DirOps.GetDirInfo(srcPath, options); /*************************************************************************/ totalFiles = info.totalFiles; // Show the the status of the background copying if (options.HasFlag(DirOps.Options.TopDirectoryOnly)) { text = String.Format("Copying {0} files ({1})", info.totalFiles, GetBytesReadable(info.totalBytes)); } else { text = String.Format("Copying {0} files, {1} folders ({2})", info.totalFiles, info.totalDirs, GetBytesReadable(info.totalBytes)); if (info.badLinks.Count() > 0) { text += String.Format(" - {0} bad links", info.badLinks.Count()); } } lblStatus.Text = text; DirOps.DirInfo inf = DirOps.CountDirs(srcPath); text = String.Format("Contains {0} files, {1} folders", inf.totalFiles, inf.totalDirs); MessageBox.Show(text); ProgressBar.Visible = true; lblPct.Visible = true; backgroundWorker1.RunWorkerAsync(); // Copy the source directory to the destination directory asynchronously info = await DirOps.AsyncDirectoryCopy(srcPath, dstPath, progressCallback, options); Task.WaitAll(); // Is this needed? text = String.Format("Done!\nCopyied {0} files, in {1} folders ({2})", info.totalFiles, info.totalDirs, GetBytesReadable(info.totalBytes)); if (info.badLinks.Count() > 0) { text += String.Format(" - {0} bad links", info.badLinks.Count()); } MessageBox.Show(text); /*************************** * Need to stop the background worker here if it's running */ backgroundWorker1.CancelAsync(); lblStatus.Text = "Ready"; int endTotalFiles = fileCount; //ProgressBar.Visible = false; //lblPct.Visible = false; }
private void progressCallback(DirOps.DirInfo obj) { fileCount = obj.totalFiles; }