public static bool?CanWrite(this FileConflictAction action, string source, string destination) { if (File.Exists(source) == false) { return(null); } if (File.Exists(destination) == false) { return(true); } if (action.HasFlag(FileConflictAction.Overwrite)) { return(true); } if (action.HasFlag(FileConflictAction.Newer)) { return(new FileInfo(source).LastWriteTime > new FileInfo(destination).LastWriteTime); } return(false); }
private FileTraverseOperation CopyOrMoveOperation(string from, string to, bool move) { if (from == to) { if (move) { return(null); } to = to.CopyableFileName(); } return(new FileTraverseOperation(from, async(path) => { var dest = path.Replace(from, to); if (path == dest) { return false; } if (new FileInfo(path).Attributes.HasFlag(FileAttributes.Directory)) { try { if (Directory.Exists(dest) == false) { Directory.CreateDirectory(dest); } if (move) { Directory.Delete(path); } } catch (UnauthorizedAccessException) { if (await ConfirmContinueOperationAsync(Properties.Resources.Copy_UnauthorizedException) == false) { return true; } } catch (IOException) { } return false; } if (conflictAction.HasFlag(FileConflictAction.ApplyToAll) == false) { if ((conflictAction = await ConfirmFileConflictActionAsync(path, dest)) == FileConflictAction.None) { return true; } } if (conflictAction.CanWrite(path, dest) == true) { var restoreReadOnly = false; if (File.Exists(dest)) { var attributes = new FileInfo(dest).Attributes; if (attributes.HasFlag(FileAttributes.ReadOnly)) { attributes &= ~FileAttributes.ReadOnly; File.SetAttributes(dest, attributes); restoreReadOnly = true; } } try { Directory.CreateDirectory(Path.GetDirectoryName(dest)); if (move) { if (path != dest) { File.Delete(dest); File.Move(path, dest); } } else { File.Copy(path, dest, true); } } catch (UnauthorizedAccessException) { return await ConfirmContinueOperationAsync(Properties.Resources.Copy_UnauthorizedException); } catch (IOException) { return await ConfirmContinueOperationAsync(Properties.Resources.Copy_UnauthorizedException); } if (restoreReadOnly) { var attributes = new FileInfo(dest).Attributes; attributes |= FileAttributes.ReadOnly; File.SetAttributes(dest, attributes); } } return false; })); }