public RoboCopyResults BuildResults(int exitCode) { var res = new RoboCopyResults(); res.Status = new RoboCopyExitStatus(exitCode); var statisticLines = GetStatisticLines(); if (statisticLines.Count >= 1) { res.DirectoriesStatistic = Statistic.Parse(statisticLines[0]); } if (statisticLines.Count >= 2) { res.FilesStatistic = Statistic.Parse(statisticLines[1]); } if (statisticLines.Count >= 3) { res.BytesStatistic = Statistic.Parse(statisticLines[2]); } if (statisticLines.Count >= 6) { res.SpeedStatistic = SpeedStatistic.Parse(statisticLines[4], statisticLines[5]); } res.LogLines = outputLines.ToArray(); return(res); }
public Task Start(string domain = "", string username = "", string password = "") { Debugger.Instance.DebugMessage("RoboCommand started execution."); hasError = false; isRunning = true; var tokenSource = new CancellationTokenSource(); CancellationToken cancellationToken = tokenSource.Token; resultsBuilder = new Results.ResultsBuilder(); results = null; // make sure source path is valid if (!Directory.Exists(CopyOptions.Source)) { Debugger.Instance.DebugMessage("The Source directory does not exist."); hasError = true; OnCommandError?.Invoke(this, new CommandErrorEventArgs("The Source directory does not exist.")); Debugger.Instance.DebugMessage("RoboCommand execution stopped due to error."); tokenSource.Cancel(true); } #region Create Destination Directory try { var dInfo = Directory.CreateDirectory(CopyOptions.Destination); if (!dInfo.Exists) { Debugger.Instance.DebugMessage("The destination directory does not exist."); hasError = true; OnCommandError?.Invoke(this, new CommandErrorEventArgs("The Destination directory is invalid.")); Debugger.Instance.DebugMessage("RoboCommand execution stopped due to error."); tokenSource.Cancel(true); } } catch (Exception ex) { Debugger.Instance.DebugMessage(ex.Message); hasError = true; OnCommandError?.Invoke(this, new CommandErrorEventArgs("The Destination directory is invalid.")); Debugger.Instance.DebugMessage("RoboCommand execution stopped due to error."); tokenSource.Cancel(true); } #endregion backupTask = Task.Factory.StartNew(() => { cancellationToken.ThrowIfCancellationRequested(); process = new Process(); if (!string.IsNullOrEmpty(domain)) { Debugger.Instance.DebugMessage(string.Format("RoboCommand running under domain - {0}", domain)); process.StartInfo.Domain = domain; } if (!string.IsNullOrEmpty(username)) { Debugger.Instance.DebugMessage(string.Format("RoboCommand running under username - {0}", username)); process.StartInfo.UserName = username; } if (!string.IsNullOrEmpty(password)) { Debugger.Instance.DebugMessage("RoboCommand password entered."); var ssPwd = new System.Security.SecureString(); for (int x = 0; x < password.Length; x++) { ssPwd.AppendChar(password[x]); } process.StartInfo.Password = ssPwd; } Debugger.Instance.DebugMessage("Setting RoboCopy process up..."); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.CreateNoWindow = true; process.StartInfo.FileName = Configuration.RoboCopyExe; process.StartInfo.Arguments = GenerateParameters(); process.OutputDataReceived += process_OutputDataReceived; process.ErrorDataReceived += process_ErrorDataReceived; process.EnableRaisingEvents = true; process.Exited += Process_Exited; Debugger.Instance.DebugMessage("RoboCopy process started."); process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); results = resultsBuilder.BuildResults(process?.ExitCode ?? -1); Debugger.Instance.DebugMessage("RoboCopy process exited."); }, cancellationToken, TaskCreationOptions.LongRunning, PriorityScheduler.BelowNormal); Task continueWithTask = backupTask.ContinueWith((continuation) => { if (!hasError) { // backup is complete if (OnCommandCompleted != null) { OnCommandCompleted(this, new RoboCommandCompletedEventArgs(results)); isRunning = false; } } Stop(); }, cancellationToken); return(continueWithTask); }
public RoboCommandCompletedEventArgs(Results.RoboCopyResults results) { this.Results = results; }