private Result GetOverwriteResult(SourceTarget source , IFileInfo target) { Result result = Result.Yes; if (target.Exists) { result = Result.No; if (Overwrite == OverwriteOptionConstants.Ask) { Int64 startTicks = DateTime.Now.Ticks; result = UIServices.ShowMessageBox("Overwrite \"" + target.FullName + "\"\nfrom \"" + source.SourceFile.FullName + "\"?", "Overwrite?", Buttons.YesNoCancel, Icon.Question); Int64 endTicks = DateTime.Now.Ticks; TimeSpan span = new TimeSpan(endTicks - startTicks); CopyPaused?.Invoke(this, new EventArgs <TimeSpan>(span)); } else if (Overwrite == OverwriteOptionConstants.Always) { result = Result.Yes; } } return(result); }
private void Copy(CancellationToken cancellationToken) { RemainingTimeCalculator.Start(); OnModelProgressMaxChanged(this, EventArgs.Empty); OnModelProgressValueChanged(this, EventArgs.Empty); Model.SizeChanged += OnModelProgressMaxChanged; Model.ProgressChanged += OnModelProgressValueChanged; Model.CopyPaused += OnModelCopyPaused; TaskIsRunning = true; try { Model.Copy(TargetPath, SelectedOverwriteOption, cancellationToken); } catch (Exception ex) { UIServices.ShowMessageBox(ex.Message, "Error", Buttons.OK, Icon.Error); } TaskIsRunning = false; Model.CopyPaused -= OnModelCopyPaused; Model.ProgressChanged -= OnModelProgressValueChanged; Model.SizeChanged -= OnModelProgressMaxChanged; }
public void Copy(String targetLocation , String overwrite , CancellationToken cancellationToken) { TargetLocation = targetLocation; Overwrite = overwrite; CancellationToken = cancellationToken; ProgressValue = 0; List <SourceTarget> fileInfos = new List <SourceTarget>(); foreach (String entry in Entries) { if (IOServices.Folder.Exists(entry)) { IEnumerable <String> files = IOServices.Folder.GetFiles(entry, searchOption: System.IO.SearchOption.AllDirectories); fileInfos.AddRange(files.Select(file => new SourceTarget(IOServices.GetFileInfo(file)))); } else if (IOServices.File.Exists(entry)) { fileInfos.Add(new SourceTarget(IOServices.GetFileInfo(entry))); } else { UIServices.ShowMessageBox("Something is weird about\n" + entry, "?!?", Buttons.OK, Icon.Error); return; } } ResetSize(); IDriveInfo driveInfo = IOServices.GetDriveInfo(IOServices.GetFolderInfo(TargetLocation).Root.Name.Substring(0, 1)); if (driveInfo.AvailableFreeSpace <= Size) { FileSize spaceSize = new FileSize(driveInfo.AvailableFreeSpace); FileSize bytesSize = new FileSize(Size); UIServices.ShowMessageBox($"Target is Full!{Environment.NewLine}Available: {spaceSize}{Environment.NewLine}Needed: {bytesSize}", "Target Full", Buttons.OK, Icon.Warning); return; } Copy(fileInfos); ProgressValue = 0; App.WasCopied = true; }
private void Copy(List <SourceTarget> fileInfos) { Boolean taskCancelled = false; try { foreach (SourceTarget fileInfo in fileInfos) { if (CancellationToken.IsCancellationRequested) { UIServices.ShowMessageBox("The Copy process was aborted.", String.Empty, Buttons.OK, Icon.Warning); taskCancelled = true; return; } if (PreserveSubFolders) { EnsureSubFolders(fileInfo); } else { fileInfo.TargetFolder = IOServices.GetFolderInfo(TargetLocation); } } taskCancelled = CommenceCopy(fileInfos); } catch (System.IO.IOException ioEx) { UIServices.ShowMessageBox(ioEx.Message, "?!?", Buttons.OK, Icon.Error); } catch (Exception ex) { UIServices.ShowMessageBox(ex.Message, "?!?", Buttons.OK, Icon.Error); } finally { ProgressChanged?.Invoke(this, EventArgs.Empty); if (taskCancelled == false) { UIServices.ShowMessageBox("Copy Finished.", String.Empty, Buttons.OK, Icon.Information); } } }
public void ReadXml(String fileName) { try { using (System.IO.Stream fs = IOServices.GetFileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { RecentFiles recentFiles = Serializer <RecentFiles> .Deserialize(fs); foreach (String file in recentFiles.Files) { if (IOServices.File.Exists(file)) { HashedEntries.Add(file); } } } } catch (Exception ex) { UIServices.ShowMessageBox(ex.Message, "Error", Buttons.OK, Icon.Error); } }
private Boolean CommenceCopy(List <SourceTarget> fileInfos) { foreach (SourceTarget fileInfo in fileInfos) { if (CancellationToken.IsCancellationRequested) { UIServices.ShowMessageBox("The Copy process was aborted.", String.Empty, Buttons.OK, Icon.Warning); return(true); } String path = IOServices.Path.Combine(fileInfo.TargetFolder.FullName, fileInfo.SourceFile.Name); IFileInfo targetFileInfo = IOServices.GetFileInfo(path); Result result = GetOverwriteResult(fileInfo, targetFileInfo); if (result == Result.Cancel) { return(true); } else if (result == Result.No) { m_Size -= fileInfo.SourceFile.Length; SizeChanged?.Invoke(this, EventArgs.Empty); continue; } else if (result == Result.Yes) { try { if (IOServices.File.Exists(targetFileInfo.FullName)) { IOServices.File.SetAttributes(targetFileInfo.FullName, System.IO.FileAttributes.Normal | System.IO.FileAttributes.Archive); } IOServices.File.Copy(fileInfo.SourceFile.FullName, targetFileInfo.FullName, true); } catch (Exception ex) { Int64 startTicks = DateTime.Now.Ticks; if (UIServices.ShowMessageBox(ex.Message + "\nContinue?", "Continue?", Buttons.YesNo, Icon.Question) == Result.Yes) { Int64 endTicks = DateTime.Now.Ticks; TimeSpan span = new TimeSpan(endTicks - startTicks); CopyPaused?.Invoke(this, new EventArgs <TimeSpan>(span)); continue; } else { return(true); } } ProgressValue += fileInfo.SourceFile.Length; ProgressChanged?.Invoke(this, EventArgs.Empty); } } return(false); }