internal static Exception GetIoExceptionForError(int error, string path = null) { // http://referencesource.microsoft.com/#mscorlib/system/io/__error.cs,142 string errorText = $"{NativeErrorHelper.LastErrorToString(error)} : '{path ?? XTaskStrings.NoValue}'"; switch (error) { case WinError.ERROR_FILE_NOT_FOUND: return(new FileNotFoundException(errorText, path)); case WinError.ERROR_PATH_NOT_FOUND: return(new DirectoryNotFoundException(errorText)); case WinError.ERROR_ACCESS_DENIED: // Network access doesn't throw UnauthorizedAccess in .NET case WinError.ERROR_NETWORK_ACCESS_DENIED: return(new UnauthorizedAccessException(errorText)); case WinError.ERROR_FILENAME_EXCED_RANGE: return(new PathTooLongException(errorText)); case WinError.ERROR_INVALID_DRIVE: return(new DriveNotFoundException(errorText)); case WinError.ERROR_OPERATION_ABORTED: return(new OperationCanceledException(errorText)); case WinError.ERROR_ALREADY_EXISTS: case WinError.ERROR_SHARING_VIOLATION: case WinError.ERROR_FILE_EXISTS: default: return(new IOException(errorText, NativeErrorHelper.GetHResultForWindowsError(error))); } }
/// <summary> /// Try to get the error message for GetLastError result /// </summary> public static string LastErrorToString(int error) { return(String.Format( CultureInfo.CurrentUICulture, "Error {0}: {1}", error, NativeErrorHelper.FormatMessage(error))); }
/// <summary> /// Try to get the string for an HRESULT /// </summary> public static string HResultToString(int result) { string message; if (HRESULT_FACILITY(result) == FACILITY_WIN32) { // Win32 Error, extract the code message = NativeErrorHelper.FormatMessage(HRESULT_CODE(result)); } else { // Hope that we get a rational IErrorInfo Exception exception = Marshal.GetExceptionForHR(result); message = exception.Message; } return(String.Format( CultureInfo.CurrentUICulture, "HRESULT {0:D} [0x{0:X}]: {1}", result, message)); }
/// <summary> /// Set the attributes on the given path if possible. /// </summary> /// <remarks> /// This is far greater perf than System.IO as it can skip all of the validation and normalization. /// </remarks> /// <exception cref="System.UnauthorizedAccessException">Thrown if the current user does not have rights to the specified path.</exception> internal static bool TrySetFileAttributes(string path, FileAttributes attributes) { if (!Private.SetFileAttributesW(path, (uint)attributes)) { int lastError = Marshal.GetLastWin32Error(); switch (lastError) { case WinError.ERROR_ACCESS_DENIED: case WinError.ERROR_NETWORK_ACCESS_DENIED: throw new UnauthorizedAccessException(String.Format(CultureInfo.InvariantCulture, "{0} : '{1}'", NativeErrorHelper.LastErrorToString(lastError), path)); } return(false); } return(true); }
private static FileAttributes TryGetFileAttributesPrivate(string path) { path = Paths.AddExtendedPrefix(path); FileAttributes result = Private.GetFileAttributesW(path); if (result == InvalidFileAttributes) { int error = Marshal.GetLastWin32Error(); switch (error) { case WinError.ERROR_ACCESS_DENIED: case WinError.ERROR_NETWORK_ACCESS_DENIED: throw new UnauthorizedAccessException(String.Format(CultureInfo.InvariantCulture, "{0} : '{1}'", NativeErrorHelper.LastErrorToString(error), path)); } } return(result); }