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 ShellDelete(string FullPath, UIOptionInternal ShowUI, RecycleOption recycle, UICancelOption OnUserCancel, FileOrDirectory FileOrDirectory) { Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags operationFlags = GetOperationFlags(ShowUI); if (recycle == RecycleOption.SendToRecycleBin) { operationFlags |= Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags.FOF_ALLOWUNDO; } ShellFileOperation(Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_DELETE, operationFlags, FullPath, null, OnUserCancel, FileOrDirectory); }
private static Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags GetOperationFlags(UIOptionInternal ShowUI) { Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags flags2 = Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags.FOF_NO_CONNECTED_ELEMENTS | Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags.FOF_NOCONFIRMMKDIR; if (ShowUI == UIOptionInternal.OnlyErrorDialogs) { flags2 |= Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags.FOF_NOCONFIRMATION | Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags.FOF_SILENT; } return flags2; }
private static void DeleteFileInternal(string file, UIOptionInternal showUI, RecycleOption recycle, UICancelOption onUserCancel) { VerifyRecycleOption("recycle", recycle); VerifyUICancelOption("onUserCancel", onUserCancel); string path = NormalizeFilePath(file, "file"); new FileIOPermission(FileIOPermissionAccess.Write, path).Demand(); ThrowIfDevicePath(path); if (!File.Exists(path)) { throw ExceptionUtils.GetFileNotFoundException(file, "IO_FileNotFound_Path", new string[] { file }); } if ((showUI != UIOptionInternal.NoUI) && Environment.UserInteractive) { ShellDelete(path, showUI, recycle, onUserCancel, FileOrDirectory.File); } else { File.Delete(path); } }
private static void DeleteDirectoryInternal(string directory, DeleteDirectoryOption onDirectoryNotEmpty, UIOptionInternal showUI, RecycleOption recycle, UICancelOption onUserCancel) { VerifyDeleteDirectoryOption("onDirectoryNotEmpty", onDirectoryNotEmpty); VerifyRecycleOption("recycle", recycle); VerifyUICancelOption("onUserCancel", onUserCancel); string fullPath = Path.GetFullPath(directory); DemandDirectoryPermission(fullPath, FileIOPermissionAccess.Write); ThrowIfDevicePath(fullPath); if (!Directory.Exists(fullPath)) { throw ExceptionUtils.GetDirectoryNotFoundException("IO_DirectoryNotFound_Path", new string[] { directory }); } if (IsRoot(fullPath)) { throw ExceptionUtils.GetIOException("IO_DirectoryIsRoot_Path", new string[] { directory }); } if ((showUI != UIOptionInternal.NoUI) && Environment.UserInteractive) { ShellDelete(fullPath, showUI, recycle, onUserCancel, FileOrDirectory.Directory); } else { Directory.Delete(fullPath, onDirectoryNotEmpty == DeleteDirectoryOption.DeleteAllContents); } }
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); } }