private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { progressWindow.Close(); Visible = true; if (e.Error != null) { MessageBox.Show(this, $"An unexpected error occured: {e.Error}", "Error", MessageBoxButtons.OK); } else { FolderMoveResult result = (FolderMoveResult)e.Result; if (result.HasFlag(enumFolderMoveResult.CleanDestination)) { Directory.Delete(txbDestination.Text, true); Directory.CreateDirectory(txbDestination.Text); } else if (result.HasFlag(enumFolderMoveResult.CleanDestination)) { Directory.Delete(txbDestination.Text, true); } if (result.HasFlag(enumFolderMoveResult.DeleteLink)) { File.Delete(txbSource.Text); } if (result.HasFlag(enumFolderMoveResult.RevertMoveSource)) { Directory.Move(Path.Combine(Path.GetTempPath(), Path.GetFileName(txbSource.Text) + ".old"), txbSource.Text); } if (e.Cancelled) { MessageBox.Show(this, $"Operation canceled by user.", "Canceled", MessageBoxButtons.OK); } else if (!result.IsFlag(enumFolderMoveResult.Success)) { MessageBox.Show(this, $"Folder\n\t{txbSource.Text}\ncould not be copyed.", "Failed", MessageBoxButtons.OK); } else { MessageBox.Show(this, $"Folder\n\t{txbSource.Text}\nis copyed to\n\t{txbDestination.Text}", "Success", MessageBoxButtons.OK); } } }
private void Worker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = ((BackgroundWorker)sender); FolderMoveAction action = ((FolderMoveAction)e.Argument); FolderMoveResult result = new FolderMoveResult(); e.Result = result; worker.ReportProgress(0, "calculation folder size"); var folderCount = GetDirectoryCount(action.SourcePath); var fileSizes = GetDirectorySize(action.SourcePath); long progressMax = folderCount + fileSizes; long progress = 0; if (!Directory.Exists(action.DestinationPath)) { worker.ReportProgress(0, $"creating folder {action.DestinationPath}"); Directory.CreateDirectory(action.DestinationPath); result.SetFlag(enumFolderMoveResult.DeleteDestination); } // createing directory tree and copy files var directories = new Stack <string>(); directories.Push(action.SourcePath); result.SetFlag(enumFolderMoveResult.CleanDestination); while (directories.Count > 0 && !worker.CancellationPending) { var basePath = directories.Pop(); foreach (var d in Directory.EnumerateDirectories(basePath)) { var newDirectory = Path.Combine(action.DestinationPath, d.Substring(action.SourcePath.Length + 1)); worker.ReportProgress(CalcProgress(progress, progressMax), $"creating folder {newDirectory}"); Directory.CreateDirectory(newDirectory); directories.Push(d); progress++; if (worker.CancellationPending) { return; } } foreach (var f in Directory.EnumerateFiles(basePath)) { var destPath = Path.Combine(action.DestinationPath, f.Substring(action.SourcePath.Length + 1)); FileInfo info = new FileInfo(f); worker.ReportProgress(CalcProgress(progress, progressMax), $"copying to {destPath}"); File.Copy(f, destPath); progress += info.Length; if (worker.CancellationPending) { return; } } } // moveing old folder if (worker.CancellationPending) { return; } worker.ReportProgress(CalcProgress(progress, progressMax), $"renaming {Path.GetFileName(action.SourcePath)} to {Path.GetFileName(action.SourcePath)}.old .."); string sourceCopy = Path.Combine(Path.GetTempPath(), Path.GetFileName(action.SourcePath) + ".old"); try { Directory.Move(action.SourcePath, sourceCopy); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); result.SetFlag(enumFolderMoveResult.RevertMoveSource); } // create link if (worker.CancellationPending) { return; } worker.ReportProgress(CalcProgress(progress, progressMax), $"creating symbolic link .."); using (Process process = new Process()) { process.StartInfo = new ProcessStartInfo { FileName = "cmd", Arguments = $"/c mklink /J \"{action.SourcePath}\" \"{action.DestinationPath}\"", UseShellExecute = false, CreateNoWindow = true, RedirectStandardError = true, RedirectStandardOutput = true, }; process.ErrorDataReceived += Process_ErrorDataReceived; process.OutputDataReceived += Process_OutputDataReceived; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); if (process.ExitCode != 0) { result.SetFlag(enumFolderMoveResult.DeleteLink); } process.Close(); } // removeing old folder if (worker.CancellationPending) { return; } Directory.Delete(sourceCopy, true); // finishing if (worker.CancellationPending) { return; } result.Reset(); }