-
Notifications
You must be signed in to change notification settings - Fork 69
Code Snippets (C#)
This section contains various C# code snippets that may be useful to anyone creating apps which use RoboSharp
public void Backup()
{
RoboCommand backup = new RoboCommand();
// events
backup.OnFileProcessed += backup_OnFileProcessed;
backup.OnCommandCompleted += backup_OnCommandCompleted;
// copy options
backup.CopyOptions.Source = Source.Text;
backup.CopyOptions.Destination = Destination.Text;
backup.CopyOptions.CopySubdirectories = true;
backup.CopyOptions.UseUnbufferedIo = true;
// select options
backup.SelectionOptions.OnlyCopyArchiveFilesAndResetArchiveFlag = true;
backup.SelectionOptions.ExcludedFiles.Add("myfile.txt");
backup.SelectionOptions.ExcludedFiles.Add("my file.txt");
// or
// var FilestoExclude = new List<string>(new[] { "myfile.txt", "my file.txt" });
// backup.SelectionOptions.ExcludedFiles.AddRange(FilestoExclude);
// same methods can be used for ExcludedDirectories
// retry options
backup.RetryOptions.RetryCount = 1;
backup.RetryOptions.RetryWaitTime = 2;
backup.Start();
}
void backup_OnFileProcessed(object sender, FileProcessedEventArgs e)
{
Dispatcher.BeginInvoke((Action)(() =>
{
CurrentOperation.Text = e.ProcessedFile.FileClass;
CurrentFile.Text = e.ProcessedFile.Name;
CurrentSize.Text = e.ProcessedFile.Size.ToString();
}));
}
void backup_OnCommandCompleted(object sender, RoboCommandCompletedEventArgs e)
{
Dispatcher.BeginInvoke((Action)(() =>
{
MessageBox.Show("Backup Complete!");
}));
}PR #127 adds some great new methods for handling results which can be used when running multiple jobs. This then allows you to view combined totals for files, directories and bytes as well as averages for speed, along with log, status and exit code information for each job
Basic usage example shown below - the new methods are all demonstrated in more detail in the included example BackupApp
private Results.RoboCopyResultsList JobResults = new Results.RoboCopyResultsList();
void copy_OnCommandCompleted(object sender, RoboCommandCompletedEventArgs e)
{
Dispatcher.BeginInvoke((Action)(() =>
{
OptionsGrid.IsEnabled = true;
ProgressGrid.IsEnabled = false;
var results = e.Results;
JobResults.Add(e.Results);
}));
}
Results.RoboCopyResults result = (Results.RoboCopyResults)this.ListBox_JobResults.SelectedItem;
string NL = Environment.NewLine;
lbl_SelectedItemTotals.Content = $"Selected Job:" +
$"{NL}Source: {result?.Source ?? ""}" +
$"{NL}Destination: {result?.Destination ?? ""}" +
$"{NL}Total Directories: {result?.DirectoriesStatistic?.Total ?? 0}" +
$"{NL}Total Files: {result?.FilesStatistic?.Total ?? 0}" +
$"{NL}Total Size (bytes): {result?.BytesStatistic?.Total ?? 0}" +
$"{NL}Speed (Bytes/Second): {result?.SpeedStatistic?.BytesPerSec ?? 0}" +
$"{NL}Speed (MB/Min): {result?.SpeedStatistic?.MegaBytesPerMin ?? 0}" +
$"{NL}{result?.Status.ToString() ?? ""}";
string NL = Environment.NewLine;
lbl_OverallTotals.Content = $"Job History:" +
$"{NL}Total Directories: {JobResults.DirectoriesStatistic.Total}" +
$"{NL}Total Files: {JobResults.FilesStatistic.Total}" +
$"{NL}Total Size (bytes): {JobResults.BytesStatistic.Total}" +
$"{NL}Speed (Bytes/Second): {JobResults.SpeedStatistic.BytesPerSec}" +
$"{NL}Speed (MB/Min): {JobResults.SpeedStatistic.MegaBytesPerMin}" +
$"{NL}Any Jobs Cancelled: {(JobResults.Status.WasCancelled ? "YES" : "NO")}" +
$"{NL}{JobResults.Status.ToString()}";N.B. The above has been superseded by changes in PR #127 but is still available to be used if required
This is useful if you are running multiple RoboCopy tasks as it allows you to add all the statistics to each other to generating overall results
RoboSharp.Results.Statistic FileStats = new RoboSharp.Results.Statistic();
RoboSharp.Results.Statistic DirStats = new RoboSharp.Results.Statistic();
test = new RoboCommand();
// Run first task and add results
test.CopyOptions.Source = @"C:\SOURCE_1";
test.CopyOptions.Destination = @"C:\DESTINATION";
RoboSharp.Results.RoboCopyResults results1 = await test.StartAsync();
FileStats.AddStatistic(results1.FilesStatistic);
DirStats.AddStatistic(results1.DirectoriesStatistic);
// Run second task and add results
test.CopyOptions.Source = @"C:\SOURCE_2";
test.CopyOptions.Destination = @"C:\DESTINATION";
RoboSharp.Results.RoboCopyResults results2 = await test.StartAsync();
FileStats.AddStatistic(results2.FilesStatistic);
DirStats.AddStatistic(results2.DirectoriesStatistic);You could also use .AddStatistic in the OnCommandCompleted event e.g.
void copy_OnCommandCompleted(object sender, RoboCommandCompletedEventArgs e)
{
this.BeginInvoke((Action)(() =>
{
// Get robocopy results
RoboSharp.Results.RoboCopyResults AnalysisResults = e.Results;
FileStats.AddStatistic(AnalysisResults.FilesStatistic);
}));
}N.B. The above has been replaced by improved methods in PR #127
RoboCommand
- IRoboCommand Interface- RoboCommand
- CopyOptions
- JobOptions
- LoggingOptions
- RetryOptions
- SelectionOptions
- RoboSharpConfiguration
- JobFile
RoboQueue
- RoboQueueResults Objects
- RoboCopyResults- IResults Interface
- RoboCopyResultsList
- IRoboCopyResultsList Interface
ExitStatus Objects
- RoboCopyExitStatus- RoboCopyCombinedExitStatus
- IRoboCopyCombinedExitStatus Interface
Progress Estimator Objects
- IProgressEstimator Interface- ProgressEstimator
- RoboQueueProgressEstimator