private static void ShellCopyOrMove(CopyOrMove Operation, FileOrDirectory TargetType, string FullSourcePath, string FullTargetPath, UIOptionInternal ShowUI, UICancelOption OnUserCancel) { Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType type; if (Operation == CopyOrMove.Copy) { type = Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_COPY; } else { type = Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_MOVE; } Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags operationFlags = GetOperationFlags(ShowUI); string fullSource = FullSourcePath; if (TargetType == FileOrDirectory.Directory) { if (Directory.Exists(FullTargetPath)) { fullSource = Path.Combine(FullSourcePath, "*"); } else { Directory.CreateDirectory(GetParentPath(FullTargetPath)); } } ShellFileOperation(type, operationFlags, fullSource, FullTargetPath, OnUserCancel, TargetType); if ((((Operation == CopyOrMove.Move) & (TargetType == FileOrDirectory.Directory)) && Directory.Exists(FullSourcePath)) && ((Directory.GetDirectories(FullSourcePath).Length == 0) && (Directory.GetFiles(FullSourcePath).Length == 0))) { Directory.Delete(FullSourcePath, false); } }
private static void FxCopyOrMoveDirectory(CopyOrMove operation, string sourceDirectoryPath, string targetDirectoryPath, bool overwrite) { if (((operation == CopyOrMove.Move) & !Directory.Exists(targetDirectoryPath)) & IsOnSameDrive(sourceDirectoryPath, targetDirectoryPath)) { Directory.CreateDirectory(GetParentPath(targetDirectoryPath)); try { Directory.Move(sourceDirectoryPath, targetDirectoryPath); return; } catch (IOException) { } catch (UnauthorizedAccessException) { } } Directory.CreateDirectory(targetDirectoryPath); DirectoryNode sourceDirectoryNode = new DirectoryNode(sourceDirectoryPath, targetDirectoryPath); ListDictionary exceptions = new ListDictionary(); CopyOrMoveDirectoryNode(operation, sourceDirectoryNode, overwrite, exceptions); if (exceptions.Count > 0) { IOException exception3 = new IOException(Utils.GetResourceString("IO_CopyMoveRecursive")); IDictionaryEnumerator enumerator = exceptions.GetEnumerator(); while (enumerator.MoveNext()) { DictionaryEntry current = (DictionaryEntry) enumerator.Current; exception3.Data.Add(current.Key, current.Value); } throw exception3; } }
private static void CopyOrMoveDirectoryNode(CopyOrMove Operation, DirectoryNode SourceDirectoryNode, bool Overwrite, ListDictionary Exceptions) { try { if (!Directory.Exists(SourceDirectoryNode.TargetPath)) { Directory.CreateDirectory(SourceDirectoryNode.TargetPath); } } catch (Exception exception) { if ((!(exception is IOException) && !(exception is UnauthorizedAccessException)) && ((!(exception is DirectoryNotFoundException) && !(exception is NotSupportedException)) && !(exception is SecurityException))) { throw; } Exceptions.Add(SourceDirectoryNode.Path, exception.Message); return; } if (!Directory.Exists(SourceDirectoryNode.TargetPath)) { Exceptions.Add(SourceDirectoryNode.TargetPath, ExceptionUtils.GetDirectoryNotFoundException("IO_DirectoryNotFound_Path", new string[] { SourceDirectoryNode.TargetPath })); } else { foreach (string str in Directory.GetFiles(SourceDirectoryNode.Path)) { try { CopyOrMoveFile(Operation, str, Path.Combine(SourceDirectoryNode.TargetPath, Path.GetFileName(str)), Overwrite, UIOptionInternal.NoUI, UICancelOption.ThrowException); } catch (Exception exception2) { if ((!(exception2 is IOException) && !(exception2 is UnauthorizedAccessException)) && (!(exception2 is SecurityException) && !(exception2 is NotSupportedException))) { throw; } Exceptions.Add(str, exception2.Message); } } foreach (DirectoryNode node in SourceDirectoryNode.SubDirs) { CopyOrMoveDirectoryNode(Operation, node, Overwrite, Exceptions); } if (Operation == CopyOrMove.Move) { try { Directory.Delete(SourceDirectoryNode.Path, false); } catch (Exception exception3) { if ((!(exception3 is IOException) && !(exception3 is UnauthorizedAccessException)) && (!(exception3 is SecurityException) && !(exception3 is DirectoryNotFoundException))) { throw; } Exceptions.Add(SourceDirectoryNode.Path, exception3.Message); } } } }
private static void CopyOrMoveFile(CopyOrMove operation, string sourceFileName, string destinationFileName, bool overwrite, UIOptionInternal showUI, UICancelOption onUserCancel) { VerifyUICancelOption("onUserCancel", onUserCancel); string path = NormalizeFilePath(sourceFileName, "sourceFileName"); string str = NormalizeFilePath(destinationFileName, "destinationFileName"); FileIOPermissionAccess read = FileIOPermissionAccess.Read; if (operation == CopyOrMove.Move) { read |= FileIOPermissionAccess.Write; } new FileIOPermission(read, path).Demand(); new FileIOPermission(FileIOPermissionAccess.Write, str).Demand(); ThrowIfDevicePath(path); ThrowIfDevicePath(str); if (!File.Exists(path)) { throw ExceptionUtils.GetFileNotFoundException(sourceFileName, "IO_FileNotFound_Path", new string[] { sourceFileName }); } if (Directory.Exists(str)) { throw ExceptionUtils.GetIOException("IO_DirectoryExists_Path", new string[] { destinationFileName }); } Directory.CreateDirectory(GetParentPath(str)); if ((showUI != UIOptionInternal.NoUI) && Environment.UserInteractive) { ShellCopyOrMove(operation, FileOrDirectory.File, path, str, showUI, onUserCancel); } else if ((operation == CopyOrMove.Copy) || path.Equals(str, StringComparison.OrdinalIgnoreCase)) { File.Copy(path, str, overwrite); } else if (overwrite) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) { new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); try { if (!Microsoft.VisualBasic.CompilerServices.NativeMethods.MoveFileEx(path, str, 11)) { ThrowWinIOError(Marshal.GetLastWin32Error()); } } catch (Exception) { throw; } finally { CodeAccessPermission.RevertAssert(); } } else { File.Delete(str); File.Move(path, str); } } else { File.Move(path, str); } }
private static void CopyOrMoveDirectory(CopyOrMove operation, string sourceDirectoryName, string destinationDirectoryName, bool overwrite, UIOptionInternal showUI, UICancelOption onUserCancel) { VerifyUICancelOption("onUserCancel", onUserCancel); string fullDirectoryPath = NormalizePath(sourceDirectoryName); string str2 = NormalizePath(destinationDirectoryName); FileIOPermissionAccess read = FileIOPermissionAccess.Read; if (operation == CopyOrMove.Move) { read |= FileIOPermissionAccess.Write; } DemandDirectoryPermission(fullDirectoryPath, read); DemandDirectoryPermission(str2, FileIOPermissionAccess.Write | FileIOPermissionAccess.Read); ThrowIfDevicePath(fullDirectoryPath); ThrowIfDevicePath(str2); if (!Directory.Exists(fullDirectoryPath)) { throw ExceptionUtils.GetDirectoryNotFoundException("IO_DirectoryNotFound_Path", new string[] { sourceDirectoryName }); } if (IsRoot(fullDirectoryPath)) { throw ExceptionUtils.GetIOException("IO_DirectoryIsRoot_Path", new string[] { sourceDirectoryName }); } if (File.Exists(str2)) { throw ExceptionUtils.GetIOException("IO_FileExists_Path", new string[] { destinationDirectoryName }); } if (str2.Equals(fullDirectoryPath, StringComparison.OrdinalIgnoreCase)) { throw ExceptionUtils.GetIOException("IO_SourceEqualsTargetDirectory", new string[0]); } if (((str2.Length > fullDirectoryPath.Length) && str2.Substring(0, fullDirectoryPath.Length).Equals(fullDirectoryPath, StringComparison.OrdinalIgnoreCase)) && (str2[fullDirectoryPath.Length] == Path.DirectorySeparatorChar)) { throw ExceptionUtils.GetInvalidOperationException("IO_CyclicOperation", new string[0]); } if ((showUI != UIOptionInternal.NoUI) && Environment.UserInteractive) { ShellCopyOrMove(operation, FileOrDirectory.Directory, fullDirectoryPath, str2, showUI, onUserCancel); } else { FxCopyOrMoveDirectory(operation, fullDirectoryPath, str2, overwrite); } }