Throw correct exceptions for IO errors

pull/405/head
NextTurn 2019-07-08 00:00:00 +08:00
parent 368b99d8a1
commit b86a028392
No known key found for this signature in database
GPG Key ID: 17A0D50ADDE1A0C4
1 changed files with 10 additions and 2 deletions

View File

@ -1,5 +1,5 @@
#if !NETCOREAPP
using System.ComponentModel;
using System;
#endif
using System.IO;
#if !NETCOREAPP
@ -20,12 +20,20 @@ namespace winsw.Util
if (!NativeMethods.MoveFileEx(sourceFilePath, destFilePath, NativeMethods.MOVEFILE_REPLACE_EXISTING | NativeMethods.MOVEFILE_COPY_ALLOWED))
{
throw new Win32Exception();
throw GetExceptionForLastWin32Error(sourceFilePath);
}
#endif
}
#if !NETCOREAPP
private static Exception GetExceptionForLastWin32Error(string path) => Marshal.GetLastWin32Error() switch
{
2 => new FileNotFoundException(null, path), // ERROR_FILE_NOT_FOUND
3 => new DirectoryNotFoundException(), // ERROR_PATH_NOT_FOUND
5 => new UnauthorizedAccessException(), // ERROR_ACCESS_DENIED
_ => new IOException()
};
private static class NativeMethods
{
internal const uint MOVEFILE_REPLACE_EXISTING = 0x01;