public static void Move(string sourceDirName, string destDirName) { // sourceDirName and destDirName validation in Path.GetFullPath() sourceDirName = Path.GetFullPath(sourceDirName); destDirName = Path.GetFullPath(destDirName); bool tryCopyAndDelete = false; Object srcRecord = FileSystemManager.AddToOpenList(sourceDirName); try { // Make sure sourceDir is actually a directory if (!Exists(sourceDirName)) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } // If Move() returns false, we'll try doing copy and delete to accomplish the move tryCopyAndDelete = !NativeIO.Move(sourceDirName, destDirName); } finally { FileSystemManager.RemoveFromOpenList(srcRecord); } if (tryCopyAndDelete) { RecursiveCopyAndDelete(sourceDirName, destDirName); } }
// Moves a specified file to a new location and potentially a new file name. // This method does work across volumes. // // The caller must have certain FileIOPermissions. The caller must // have Read and Write permission to // sourceFileName and Write // permissions to destFileName. // public static void Move(String sourceFileName, String destFileName) { // sourceFileName and destFileName validation in Path.GetFullPath() sourceFileName = Path.GetFullPath(sourceFileName); destFileName = Path.GetFullPath(destFileName); bool tryCopyAndDelete = false; // We only need to lock the source, not the dest because if dest is taken // Move() will failed at the driver's level anyway. (there will be no conflict even if // another thread is creating dest, as only one of the operations will succeed -- // the native calls are atomic) Object srcRecord = FileSystemManager.AddToOpenList(sourceFileName); try { if (!Exists(sourceFileName)) { throw new IOException("", (int)IOException.IOExceptionErrorCode.FileNotFound); } //We'll try copy and deleting if Move returns false tryCopyAndDelete = !NativeIO.Move(sourceFileName, destFileName); } finally { FileSystemManager.RemoveFromOpenList(srcRecord); } if (tryCopyAndDelete) { Copy(sourceFileName, destFileName, false, true); } }