public static void Move(String sourceDirName, String destDirName) { if (sourceDirName == null) { throw new ArgumentNullException("sourceDirName"); } if (sourceDirName.Length == 0) { throw new ArgumentException(SR.Argument_EmptyFileName, "sourceDirName"); } if (destDirName == null) { throw new ArgumentNullException("destDirName"); } if (destDirName.Length == 0) { throw new ArgumentException(SR.Argument_EmptyFileName, "destDirName"); } Contract.EndContractBlock(); String fullsourceDirName = Path.GetFullPath(sourceDirName); String sourcePath = EnsureTrailingDirectorySeparator(fullsourceDirName); if (PathInternal.IsDirectoryTooLong(sourcePath)) { throw new PathTooLongException(SR.IO_PathTooLong); } String fulldestDirName = Path.GetFullPath(destDirName); String destPath = EnsureTrailingDirectorySeparator(fulldestDirName); if (PathInternal.IsDirectoryTooLong(destPath)) { throw new PathTooLongException(SR.IO_PathTooLong); } StringComparison pathComparison = PathInternal.StringComparison; if (String.Equals(sourcePath, destPath, pathComparison)) { throw new IOException(SR.IO_SourceDestMustBeDifferent); } String sourceRoot = Path.GetPathRoot(sourcePath); String destinationRoot = Path.GetPathRoot(destPath); if (!String.Equals(sourceRoot, destinationRoot, pathComparison)) { throw new IOException(SR.IO_SourceDestMustHaveSameRoot); } FileSystem.Current.MoveDirectory(fullsourceDirName, fulldestDirName); }
public override void CreateDirectory(string fullPath) { if (PathInternal.IsDirectoryTooLong(fullPath)) { throw new PathTooLongException(SR.IO_PathTooLong); } // We can save a bunch of work if the directory we want to create already exists. This also // saves us in the case where sub paths are inaccessible (due to ERROR_ACCESS_DENIED) but the // final path is accessable and the directory already exists. For example, consider trying // to create c:\Foo\Bar\Baz, where everything already exists but ACLS prevent access to c:\Foo // and c:\Foo\Bar. In that case, this code will think it needs to create c:\Foo, and c:\Foo\Bar // and fail to due so, causing an exception to be thrown. This is not what we want. if (DirectoryExists(fullPath)) { return; } List <string> stackDir = new List <string>(); // Attempt to figure out which directories don't exist, and only // create the ones we need. Note that InternalExists may fail due // to Win32 ACL's preventing us from seeing a directory, and this // isn't threadsafe. bool somepathexists = false; int length = fullPath.Length; // We need to trim the trailing slash or the code will try to create 2 directories of the same name. if (length >= 2 && PathHelpers.EndsInDirectorySeparator(fullPath)) { length--; } int lengthRoot = PathInternal.GetRootLength(fullPath); if (length > lengthRoot) { // Special case root (fullpath = X:\\) int i = length - 1; while (i >= lengthRoot && !somepathexists) { String dir = fullPath.Substring(0, i + 1); if (!DirectoryExists(dir)) // Create only the ones missing { stackDir.Add(dir); } else { somepathexists = true; } while (i > lengthRoot && !PathInternal.IsDirectorySeparator(fullPath[i])) { i--; } i--; } } int count = stackDir.Count; // If we were passed a DirectorySecurity, convert it to a security // descriptor and set it in he call to CreateDirectory. Interop.mincore.SECURITY_ATTRIBUTES secAttrs = default(Interop.mincore.SECURITY_ATTRIBUTES); bool r = true; int firstError = 0; String errorString = fullPath; // If all the security checks succeeded create all the directories while (stackDir.Count > 0) { String name = stackDir[stackDir.Count - 1]; stackDir.RemoveAt(stackDir.Count - 1); r = Interop.mincore.CreateDirectory(name, ref secAttrs); if (!r && (firstError == 0)) { int currentError = Marshal.GetLastWin32Error(); // While we tried to avoid creating directories that don't // exist above, there are at least two cases that will // cause us to see ERROR_ALREADY_EXISTS here. InternalExists // can fail because we didn't have permission to the // directory. Secondly, another thread or process could // create the directory between the time we check and the // time we try using the directory. Thirdly, it could // fail because the target does exist, but is a file. if (currentError != Interop.mincore.Errors.ERROR_ALREADY_EXISTS) { firstError = currentError; } else { // If there's a file in this directory's place, or if we have ERROR_ACCESS_DENIED when checking if the directory already exists throw. if (File.InternalExists(name) || (!DirectoryExists(name, out currentError) && currentError == Interop.mincore.Errors.ERROR_ACCESS_DENIED)) { firstError = currentError; errorString = name; } } } } // We need this check to mask OS differences // Handle CreateDirectory("X:\\") when X: doesn't exist. Similarly for n/w paths. if ((count == 0) && !somepathexists) { String root = Directory.InternalGetDirectoryRoot(fullPath); if (!DirectoryExists(root)) { throw Win32Marshal.GetExceptionForWin32Error(Interop.mincore.Errors.ERROR_PATH_NOT_FOUND, root); } return; } // Only throw an exception if creating the exact directory we // wanted failed to work correctly. if (!r && (firstError != 0)) { throw Win32Marshal.GetExceptionForWin32Error(firstError, errorString); } }
public void MoveTo(String destDirName) { if (destDirName == null) { throw new ArgumentNullException(nameof(destDirName)); } if (destDirName.Length == 0) { throw new ArgumentException(SR.Argument_EmptyFileName, nameof(destDirName)); } Contract.EndContractBlock(); String fullDestDirName = Path.GetFullPath(destDirName); if (fullDestDirName[fullDestDirName.Length - 1] != Path.DirectorySeparatorChar) { fullDestDirName = fullDestDirName + PathHelpers.DirectorySeparatorCharAsString; } String fullSourcePath; if (FullPath.Length > 0 && FullPath[FullPath.Length - 1] == Path.DirectorySeparatorChar) { fullSourcePath = FullPath; } else { fullSourcePath = FullPath + PathHelpers.DirectorySeparatorCharAsString; } if (PathInternal.IsDirectoryTooLong(fullSourcePath)) { throw new PathTooLongException(SR.IO_PathTooLong); } if (PathInternal.IsDirectoryTooLong(fullDestDirName)) { throw new PathTooLongException(SR.IO_PathTooLong); } StringComparison pathComparison = PathInternal.StringComparison; if (String.Equals(fullSourcePath, fullDestDirName, pathComparison)) { throw new IOException(SR.IO_SourceDestMustBeDifferent); } String sourceRoot = Path.GetPathRoot(fullSourcePath); String destinationRoot = Path.GetPathRoot(fullDestDirName); if (!String.Equals(sourceRoot, destinationRoot, pathComparison)) { throw new IOException(SR.IO_SourceDestMustHaveSameRoot); } if (!Exists) { throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, FullPath)); } if (FileSystem.Current.DirectoryExists(fullDestDirName)) { throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, fullDestDirName)); } FileSystem.Current.MoveDirectory(FullPath, fullDestDirName); FullPath = fullDestDirName; OriginalPath = destDirName; DisplayPath = GetDisplayName(OriginalPath); // Flush any cached information about the directory. Invalidate(); }
public void MoveTo(string destDirName) { if (destDirName == null) { throw new ArgumentNullException(nameof(destDirName)); } if (destDirName.Length == 0) { throw new ArgumentException(SR.Argument_EmptyFileName, nameof(destDirName)); } Contract.EndContractBlock(); string destination = Path.GetFullPath(destDirName); string destinationWithSeparator = destination; if (destinationWithSeparator[destinationWithSeparator.Length - 1] != Path.DirectorySeparatorChar) { destinationWithSeparator = destinationWithSeparator + PathHelpers.DirectorySeparatorCharAsString; } string fullSourcePath; if (FullPath.Length > 0 && FullPath[FullPath.Length - 1] == Path.DirectorySeparatorChar) { fullSourcePath = FullPath; } else { fullSourcePath = FullPath + PathHelpers.DirectorySeparatorCharAsString; } if (PathInternal.IsDirectoryTooLong(fullSourcePath)) { throw new PathTooLongException(SR.IO_PathTooLong); } if (PathInternal.IsDirectoryTooLong(destinationWithSeparator)) { throw new PathTooLongException(SR.IO_PathTooLong); } StringComparison pathComparison = PathInternal.StringComparison; if (string.Equals(fullSourcePath, destinationWithSeparator, pathComparison)) { throw new IOException(SR.IO_SourceDestMustBeDifferent); } string sourceRoot = Path.GetPathRoot(fullSourcePath); string destinationRoot = Path.GetPathRoot(destinationWithSeparator); if (!string.Equals(sourceRoot, destinationRoot, pathComparison)) { throw new IOException(SR.IO_SourceDestMustHaveSameRoot); } // Windows will throw if the source file/directory doesn't exist, we preemptively check // to make sure our cross platform behavior matches NetFX behavior. if (!Exists && !FileSystem.Current.FileExists(FullPath)) { throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, FullPath)); } if (FileSystem.Current.DirectoryExists(destinationWithSeparator)) { throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destinationWithSeparator)); } FileSystem.Current.MoveDirectory(FullPath, destination); FullPath = destinationWithSeparator; OriginalPath = destDirName; DisplayPath = GetDisplayName(OriginalPath); // Flush any cached information about the directory. Invalidate(); }
public static void Move(string sourceDirName, string destDirName) { if (sourceDirName == null) { throw new ArgumentNullException(nameof(sourceDirName)); } if (sourceDirName.Length == 0) { throw new ArgumentException(SR.Argument_EmptyFileName, nameof(sourceDirName)); } if (destDirName == null) { throw new ArgumentNullException(nameof(destDirName)); } if (destDirName.Length == 0) { throw new ArgumentException(SR.Argument_EmptyFileName, nameof(destDirName)); } Contract.EndContractBlock(); string fullsourceDirName = Path.GetFullPath(sourceDirName); string sourcePath = EnsureTrailingDirectorySeparator(fullsourceDirName); if (PathInternal.IsDirectoryTooLong(sourcePath)) { throw new PathTooLongException(SR.IO_PathTooLong); } string fulldestDirName = Path.GetFullPath(destDirName); string destPath = EnsureTrailingDirectorySeparator(fulldestDirName); if (PathInternal.IsDirectoryTooLong(destPath)) { throw new PathTooLongException(SR.IO_PathTooLong); } StringComparison pathComparison = PathInternal.StringComparison; if (string.Equals(sourcePath, destPath, pathComparison)) { throw new IOException(SR.IO_SourceDestMustBeDifferent); } string sourceRoot = Path.GetPathRoot(sourcePath); string destinationRoot = Path.GetPathRoot(destPath); if (!string.Equals(sourceRoot, destinationRoot, pathComparison)) { throw new IOException(SR.IO_SourceDestMustHaveSameRoot); } // Windows will throw if the source file/directory doesn't exist, we preemptively check // to make sure our cross platform behavior matches NetFX behavior. if (!FileSystem.Current.DirectoryExists(fullsourceDirName) && !FileSystem.Current.FileExists(fullsourceDirName)) { throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, fullsourceDirName)); } if (FileSystem.Current.DirectoryExists(fulldestDirName)) { throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, fulldestDirName)); } FileSystem.Current.MoveDirectory(fullsourceDirName, fulldestDirName); }