public static Task <OperationResult> CopyWithProgressAsync(
     string source,
     string destination,
     Action <Progress> progress,
     bool continueOnFailure,
     bool copyContentOfDirectory = false)
 {
     return(FileTransferHelper.CopyWithProgressAsync(source, destination, progress, continueOnFailure, CancellationToken.None, copyContentOfDirectory));
 }
 public static Task <OperationResult> CopyWithProgressAsync(
     string source,
     string destination,
     Action <Progress> progress,
     bool continueOnFailure,
     CancellationToken cancellationToken,
     bool copyContentOfDirectory = false)
 {
     return(Task.Run <OperationResult>((Func <OperationResult>)(() =>
     {
         try
         {
             return FileTransferHelper.CopyWithProgress(source, destination, progress, continueOnFailure, cancellationToken, copyContentOfDirectory);
         }
         catch
         {
             return OperationResult.Failed;
         }
     }), cancellationToken));
 }
        public static OperationResult CopyWithProgress(
            string source,
            string destination,
            Action <Progress> progress,
            bool continueOnFailure,
            CancellationToken cancellationToken,
            bool copyContentOfDirectory = false)
        {
            bool?nullable1 = source.IsPathFile();
            bool?nullable2 = nullable1 ?? throw new ArgumentException("Source parameter has to be file or directory! " + source);
            bool flag      = true;

            if (nullable2.GetValueOrDefault() == flag & nullable2.HasValue)
            {
                return(FileTransferHelper.CopyDirectoryWithProgress(source, destination, progress, continueOnFailure, cancellationToken, copyContentOfDirectory));
            }
            if (cancellationToken.IsCancellationRequested)
            {
                return(OperationResult.Cancelled);
            }
            string newFile = FileSystemHelper.CorrectFileDestinationPath(source, destination);

            return(CopyFileWithProgress(source, newFile, progress, cancellationToken));
        }
        private static OperationResult CopyDirectoryWithProgress(
            string sourceDirectory,
            string destinationDirectory,
            Action <Progress> progress,
            bool continueOnFailure,
            CancellationToken cancellationToken,
            bool copyContentOfDirectory)
        {
            DirectoryInfo d      = new DirectoryInfo(sourceDirectory.TrimEnd('\\'));
            int           length = d.FullName.Length;

            Helpers.DirectorySizeInfo rootSourceSize = d.Size();
            long totalTransferred = 0;

            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(destinationDirectory.TrimEnd('\\'));
                if (!copyContentOfDirectory)
                {
                    directoryInfo = Directory.CreateDirectory(Path.Combine(destinationDirectory, d.Name));
                }
                DateTime fileCopyStartTimestamp = DateTime.Now;
                bool     flag = false;
                foreach (DirectoryInfo enumerateDirectory in d.EnumerateDirectories("*", SearchOption.AllDirectories))
                {
                    flag = true;
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(OperationResult.Cancelled);
                    }
                    string path2 = enumerateDirectory.FullName.Substring(length + 1);
                    Directory.CreateDirectory(Path.Combine(directoryInfo.FullName, path2));
                }
                foreach (FileInfo enumerateFile in d.EnumerateFiles("*", SearchOption.AllDirectories))
                {
                    flag = true;
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(OperationResult.Cancelled);
                    }
                    string          path2           = enumerateFile.FullName.Substring(length + 1);
                    OperationResult operationResult = FileTransferHelper.CopyFileWithProgress(enumerateFile.FullName, Path.Combine(directoryInfo.FullName, path2), (Action <Progress>)(partialProgress => progress(new Progress(fileCopyStartTimestamp, totalTransferred + partialProgress.BytesTransferred, rootSourceSize.Size))), cancellationToken);
                    if (operationResult == OperationResult.Failed && !continueOnFailure)
                    {
                        return(OperationResult.Failed);
                    }
                    if (operationResult == OperationResult.Cancelled)
                    {
                        return(OperationResult.Cancelled);
                    }
                    totalTransferred += enumerateFile.Length;
                }
                if (!flag)
                {
                    progress(new Progress(fileCopyStartTimestamp, 1L, 1L));
                }
            }
            catch (Exception ex)
            {
                return(OperationResult.Failed);
            }
            return(OperationResult.Success);
        }