[System.Security.SecurityCritical] // auto-generated internal static void WinIOError(int errorCode, String maybeFullPath) { // This doesn't have to be perfect, but is a perf optimization. bool isInvalidPath = errorCode == Win32Native.ERROR_INVALID_NAME || errorCode == Win32Native.ERROR_BAD_PATHNAME; String str = GetDisplayablePath(maybeFullPath, isInvalidPath); switch (errorCode) { case Win32Native.ERROR_FILE_NOT_FOUND: if (str.Length == 0) throw new FileNotFoundException(Environment.GetResourceString("IO.FileNotFound")); else throw new FileNotFoundException(Environment.GetResourceString("IO.FileNotFound_FileName", str), str); case Win32Native.ERROR_PATH_NOT_FOUND: if (str.Length == 0) throw new DirectoryNotFoundException(Environment.GetResourceString("IO.PathNotFound_NoPathName")); else throw new DirectoryNotFoundException(Environment.GetResourceString("IO.PathNotFound_Path", str)); case Win32Native.ERROR_ACCESS_DENIED: if (str.Length == 0) throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_IODenied_NoPathName")); else throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", str)); case Win32Native.ERROR_ALREADY_EXISTS: if (str.Length == 0) goto default; throw new IOException(Environment.GetResourceString("IO.IO_AlreadyExists_Name", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath); case Win32Native.ERROR_FILENAME_EXCED_RANGE: throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong")); case Win32Native.ERROR_INVALID_DRIVE: throw new DriveNotFoundException(Environment.GetResourceString("IO.DriveNotFound_Drive", str)); case Win32Native.ERROR_INVALID_PARAMETER: throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath); case Win32Native.ERROR_SHARING_VIOLATION: if (str.Length == 0) throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_NoFileName"), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath); else throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_File", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath); case Win32Native.ERROR_FILE_EXISTS: if (str.Length == 0) goto default; throw new IOException(Environment.GetResourceString("IO.IO_FileExists_Name", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath); case Win32Native.ERROR_OPERATION_ABORTED: throw new OperationCanceledException(); default: throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath); } }
// After calling GetLastWin32Error(), it clears the last error field, // so you must save the HResult and pass it to this method. This method // will determine the appropriate exception to throw dependent on your // error, and depending on the error, insert a string into the message // gotten from the ResourceManager. internal static void WinIOError(int errorCode, String str) { switch (errorCode) { case Win32Native.ERROR_FILE_NOT_FOUND: if (str.Length == 0) { throw new FileNotFoundException(SR.IO_FileNotFound); } else { throw new FileNotFoundException(SR.Format(SR.IO_FileNotFound_FileName, str), str); } case Win32Native.ERROR_PATH_NOT_FOUND: if (str.Length == 0) { throw new DirectoryNotFoundException(SR.IO_PathNotFound_NoPathName); } else { throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, str)); } case Win32Native.ERROR_ACCESS_DENIED: if (str.Length == 0) { throw new UnauthorizedAccessException(SR.UnauthorizedAccess_IODenied_NoPathName); } else { throw new UnauthorizedAccessException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, str)); } case Win32Native.ERROR_ALREADY_EXISTS: if (str.Length == 0) { goto default; } throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, str), Win32Native.MakeHRFromErrorCode(errorCode), str); case Win32Native.ERROR_FILENAME_EXCED_RANGE: throw new PathTooLongException(SR.Format(SR.IO_PathTooLong_Path, str)); case Win32Native.ERROR_INVALID_DRIVE: throw new DriveNotFoundException(SR.Format(SR.IO_DriveNotFound_Drive, str)); case Win32Native.ERROR_INVALID_PARAMETER: throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), str); case Win32Native.ERROR_SHARING_VIOLATION: if (str.Length == 0) { throw new IOException(SR.IO_SharingViolation_NoFileName, Win32Native.MakeHRFromErrorCode(errorCode), str); } else { throw new IOException(SR.Format(SR.IO_SharingViolation_File, str), Win32Native.MakeHRFromErrorCode(errorCode), str); } case Win32Native.ERROR_FILE_EXISTS: if (str.Length == 0) { goto default; } throw new IOException(SR.Format(SR.IO_FileExists_Name, str), Win32Native.MakeHRFromErrorCode(errorCode), str); case Win32Native.ERROR_OPERATION_ABORTED: throw new OperationCanceledException(); default: throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), str); } }
internal static void Move(String sourceDirName, String destDirName) { Contract.Requires(sourceDirName != null); Contract.Requires(destDirName != null); Contract.Requires(sourceDirName.Length != 0); Contract.Requires(destDirName.Length != 0); String fullsourceDirName = LongPath.NormalizePath(sourceDirName); String sourcePath = GetDemandDir(fullsourceDirName, false); if (sourcePath.Length >= Path.MaxLongPath) { throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong")); } String fulldestDirName = LongPath.NormalizePath(destDirName); String destPath = GetDemandDir(fulldestDirName, false); if (destPath.Length >= Path.MaxLongPath) { throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong")); } new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new String[] { sourcePath }, false, false).Demand(); new FileIOPermission(FileIOPermissionAccess.Write, new String[] { destPath }, false, false).Demand(); if (String.Compare(sourcePath, destPath, StringComparison.OrdinalIgnoreCase) == 0) { throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent")); } String sourceRoot = LongPath.GetPathRoot(sourcePath); String destinationRoot = LongPath.GetPathRoot(destPath); if (String.Compare(sourceRoot, destinationRoot, StringComparison.OrdinalIgnoreCase) != 0) { throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot")); } String tempSourceDirName = Path.AddLongPathPrefix(sourceDirName); String tempDestDirName = Path.AddLongPathPrefix(destDirName); if (!Win32Native.MoveFile(tempSourceDirName, tempDestDirName)) { int hr = Marshal.GetLastWin32Error(); if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // Source dir not found { hr = Win32Native.ERROR_PATH_NOT_FOUND; __Error.WinIOError(hr, fullsourceDirName); } // This check was originally put in for Win9x (unfortunately without special casing it to be for Win9x only). We can't change the NT codepath now for backcomp reasons. if (hr == Win32Native.ERROR_ACCESS_DENIED) // WinNT throws IOException. This check is for Win9x. We can't change it for backcomp. { throw new IOException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", sourceDirName), Win32Native.MakeHRFromErrorCode(hr)); } __Error.WinIOError(hr, String.Empty); } }
// After calling GetLastWin32Error(), it clears the last error field, // so you must save the HResult and pass it to this method. This method // will determine the appropriate exception to throw dependent on your // error, and depending on the error, insert a string into the message // gotten from the ResourceManager. internal static void WinIOError(int errorCode, String str) { switch (errorCode) { case Win32Native.ERROR_FILE_NOT_FOUND: if (str.Length == 0) { throw new FileNotFoundException(Environment.GetResourceString("IO.FileNotFound")); } else { throw new FileNotFoundException(String.Format(Environment.GetResourceString("IO.FileNotFound_FileName"), str), str); } case Win32Native.ERROR_PATH_NOT_FOUND: if (str.Length == 0) { throw new DirectoryNotFoundException(Environment.GetResourceString("IO.PathNotFound_NoPathName")); } else { throw new DirectoryNotFoundException(String.Format(Environment.GetResourceString("IO.PathNotFound_Path"), str)); } case Win32Native.ERROR_ACCESS_DENIED: if (str.Length == 0) { throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_IODenied_NoPathName")); } else { throw new UnauthorizedAccessException(String.Format(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), str)); } case Win32Native.ERROR_FILENAME_EXCED_RANGE: throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong")); case Win32Native.ERROR_INVALID_PARAMETER: //BCLDebug.Assert(false, "Got an invalid parameter HRESULT from an IO method (this is ignorable, but send a simple repro to BrianGru)"); throw new IOException(GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode)); case Win32Native.ERROR_SHARING_VIOLATION: // error message. if (str.Length == 0) { throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_NoFileName")); } else { throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_File", str)); } case Win32Native.ERROR_FILE_EXISTS: if (str.Length == 0) { goto default; } throw new IOException(String.Format(Environment.GetResourceString("IO.IO_FileExists_Name"), str)); default: throw new IOException(GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode)); } }