mirror of https://github.com/winsw/winsw
Throw correct exceptions for IO errors
parent
368b99d8a1
commit
b86a028392
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue