@@ -14,14 +14,11 @@ namespace NitroxModel.OS
14
14
{
15
15
public class FileSystem
16
16
{
17
- private static readonly Lazy < FileSystem > instance = new ( ( ) =>
17
+ private static readonly Lazy < FileSystem > instance = new ( ( ) => Environment . OSVersion . Platform switch
18
18
{
19
- return Environment . OSVersion . Platform switch
20
- {
21
- PlatformID . Unix => new UnixFileSystem ( ) ,
22
- PlatformID . MacOSX => new MacFileSystem ( ) ,
23
- _ => new WinFileSystem ( )
24
- } ;
19
+ PlatformID . Unix => new UnixFileSystem ( ) ,
20
+ PlatformID . MacOSX => new MacFileSystem ( ) ,
21
+ _ => new WinFileSystem ( )
25
22
} ,
26
23
LazyThreadSafetyMode . ExecutionAndPublication ) ;
27
24
@@ -187,10 +184,12 @@ public string ZipFilesInDirectory(string dir, string outputPath = null, string f
187
184
188
185
/// <summary>
189
186
/// Replaces target file with source file. If target file does not exist then it moves the file.
187
+ /// This falls back to a copy if the target is on a different drive.
188
+ /// The source file will always be deleted.
190
189
/// </summary>
191
190
/// <param name="source">Source file to replace with.</param>
192
191
/// <param name="target">Target file to replace.</param>
193
- /// <returns>True if file was moved or replaced.</returns>
192
+ /// <returns>True if file was moved or replaced successfully .</returns>
194
193
public bool ReplaceFile ( string source , string target )
195
194
{
196
195
if ( ! File . Exists ( source ) )
@@ -210,6 +209,7 @@ public bool ReplaceFile(string source, string target)
210
209
}
211
210
catch ( IOException ex )
212
211
{
212
+ // TODO: Need to test on Linux because the ex.HResult will likely not work or be different number cross-platform.
213
213
switch ( ( uint ) ex . HResult )
214
214
{
215
215
case 0x80070498 :
@@ -222,8 +222,17 @@ public bool ReplaceFile(string source, string target)
222
222
Log . Debug ( $ "Renaming file '{ target } ' to '{ backupFileName } ' as backup plan if file replace fails") ;
223
223
File . Move ( target , backupFileName ) ;
224
224
File . Copy ( source , target ) ;
225
- File . Delete ( source ) ;
226
- File . Delete ( backupFileName ) ;
225
+
226
+ // Cleanup redundant files, ignoring errors.
227
+ try
228
+ {
229
+ File . Delete ( source ) ;
230
+ File . Delete ( backupFileName ) ;
231
+ }
232
+ catch ( Exception )
233
+ {
234
+ // ignored
235
+ }
227
236
}
228
237
catch ( Exception ex2 )
229
238
{
0 commit comments