diff --git a/src/Core/WinSWCore/Util/FileHelper.cs b/src/Core/WinSWCore/Util/FileHelper.cs index 557f86c..23efdec 100644 --- a/src/Core/WinSWCore/Util/FileHelper.cs +++ b/src/Core/WinSWCore/Util/FileHelper.cs @@ -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;