public static async Task <List <string> > DecryptFile(List <string> list) { var completedTaskList = new List <string>(); var pr = new ProgressWindow(); var source = new CancellationTokenSource(); pr.Closed += (sender, args) => { source.CancelAfter(TimeSpan.FromMilliseconds(1)); }; void ProgressCallback(double prof) => pr.ProgressBar.Value = prof; var totalSize = list.Select(x => new FileInfo(x).Length).Sum(); pr.Show(); long totalRead = 0; const double progressSize = 100.00; if (totalSize <= 0) { pr.ProgressBar.Value = 100; } foreach (var item in list) { long totalReadForFile = 0; var path = item; using (var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { var length = (int)stream.Length; var tempBytes = new byte[length]; await stream.ReadAsync(tempBytes, 0, length, source.Token); try { var read = totalRead; await DecryptStream(stream, x => { totalReadForFile = x; ProgressCallback((read + totalReadForFile) / (double)totalSize *progressSize); }, source.Token) .ContinueWith((task) => completedTaskList.Add($"{path} decrypted {DateTime.Now}"), source.Token); } catch (OperationCanceledException) { stream.Seek(0, 0); await stream.WriteAsync(tempBytes, 0, length); pr.Close(); break; } catch (Exception e) { MessageBox.Show(e.Message); } } totalRead += totalReadForFile; } return(completedTaskList); }
public static async Task <List <string> > MoveFiles(Dictionary <string, string> files) { var completedTaskList = new List <string>(); var error = false; var pr = new ProgressWindow(); var source = new CancellationTokenSource(); pr.Closed += (o, args) => { source.CancelAfter(TimeSpan.FromMilliseconds(1)); }; void ProgressCallback(double prof) => pr.ProgressBar.Value = prof; var totalSize = files.Where(file => !File.GetAttributes(file.Key).HasFlag(FileAttributes.Directory)) .Sum(file => new FileInfo(file.Key).Length); pr.Show(); long totalRead = 0; const double progressSize = 100.0; if (totalSize <= 0) { pr.ProgressBar.Value = 100; } foreach (var item in files) { long totalReadForFile = 0; var from = item.Key; var to = item.Value; if (File.GetAttributes(from).HasFlag(FileAttributes.Directory)) { Directory.CreateDirectory(to); continue; } using (var outStream = new FileStream(to, FileMode.Create, FileAccess.Write, FileShare.Read)) { using (var inStream = new FileStream(from, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { try { var read = totalRead; await CopyStream(inStream, outStream, x => { totalReadForFile = x; ProgressCallback( (read + totalReadForFile) / (double)totalSize *progressSize); }, source.Token).ContinueWith((task) => { inStream.Dispose(); inStream.Close(); File.Delete(inStream.Name); completedTaskList.Add($"{from} moved {to} {DateTime.Now}"); }, source.Token); } catch (OperationCanceledException) { outStream.Dispose(); outStream.Close(); if (File.Exists(to)) { File.Delete(to); } error = true; pr.Close(); break; } catch (Exception e) { MessageBox.Show(e.Message); error = true; } } } totalRead += totalReadForFile; } if (!error) { foreach (var file in files) { if (Directory.Exists(file.Key) && File.GetAttributes(file.Key).HasFlag(FileAttributes.Directory)) { Directory.Delete(file.Key, true); } break; } } return(completedTaskList); }