tjscience / RoboSharp

RoboSharp is a .NET wrapper for the awesome Robocopy windows application.
MIT License
216 stars 67 forks source link

Network destination path #118

Closed giovanierthal closed 2 years ago

giovanierthal commented 2 years ago

Hello.

I'm not a C# programmer. But I need to use your solution in my project.

I have the following code:

RoboCommand backup = new RoboCommand();
// events
backup.OnFileProcessed += backup_OnFileProcessed;
backup.OnCommandCompleted += backup_OnCommandCompleted;

// copy options
backup.CopyOptions.Source = @"C:\Users\myuser\Downloads\movie\";
backup.CopyOptions.Destination = @"\\Desktop-dp37hhu\myfolder\";
backup.CopyOptions.CopySubdirectories = true;
backup.CopyOptions.CopySubdirectoriesIncludingEmpty = true;            
backup.CopyOptions.UseUnbufferedIo = true;            
backup.LoggingOptions.ListOnly = true; // /l
backup.LoggingOptions.NoFileSizes = true; // /ns
backup.LoggingOptions.NoJobSummary = true; // /njs
backup.LoggingOptions.NoJobHeader = true; // /njh
backup.LoggingOptions.NoDirectoryList = true; // /ndl
backup.LoggingOptions.IncludeFullPathNames = true; // /fp
// select options           
backup.SelectionOptions.OnlyCopyArchiveFilesAndResetArchiveFlag = true;
// retry options
backup.RetryOptions.RetryCount = 1;
backup.RetryOptions.RetryWaitTime = 2;

backup.Start().Wait();

When the computer "Desktop-dp37hhu" is disconnected from the network, the following exception is raised.

Unhandled exception. System.AggregateException: One or more errors occurred. (A task was canceled.)
 ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at SyncCopy.Program.Main(String[] args) in Program.cs:line 59

Where is the problem?

PCAssistSoftware commented 2 years ago

It looks to me from you code, and the error message that the "task was canceled" because it lost contact with the destination computer you were copying to, which you would expect it to do if "Desktop-dp37hhu" is disconnected from the network as it will no longer to be able to continue the process of copying the files from source to destination.

An error being generated when destination is no longer available is not anything specific to this library, you would also get an error if you were using RoboCopy from command line to copy from "C:\Users\myuser\Downloads\movie\" to "\Desktop-dp37hhu\myfolder\" and then disconnected "Desktop-dp37hhu" part way through the copy process.

e.g.

RoboCopy"C:\Users\myuser\Downloads\movie\" "\\Desktop-dp37hhu\myfolder\" *.* /E /J /A /R:1 /W:2

Simple solution would be to ensure "Desktop-dp37hhu" is not disconnected from network when you have a copy running, or to add in some of your own code to check and make sure that "Desktop-dp37hhu" is available before starting the copy.

Checking availability of the destination during the copy process is beyond the current scope of the project and sample app because as above you would have exactly same issue if using RoboCopy via command line just as you do using it with this wrapper.

You could potentially handle the RoboCommand.OnError event (backup.OnError += copy_OnError;) to check if destination is available, prompt if it isn't and then restart copy process when it is.

Hope this make sense

PCAssistSoftware commented 2 years ago

Closed as no responses