Skip to content

Commit a7893aa

Browse files
committed
Fixed an issue on Windows where the old file is not overwritten.
1 parent 49e6624 commit a7893aa

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

LukeMauiFilePicker/FilePickerService.Windows.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Windows.Storage.Pickers;
1+
using Windows.Storage;
2+
using Windows.Storage.Pickers;
23

34
namespace LukeMauiFilePicker;
45

@@ -19,10 +20,20 @@ public partial async Task<bool> SaveFileAsync(SaveFileOptions options)
1920
var file = await picker.PickSaveFileAsync();
2021
if (file is null) { return false; }
2122

22-
using var outStr = await file.OpenStreamForWriteAsync();
23-
await options.Content.CopyToAsync(outStr);
23+
return await WriteToFileAsync(file, options.Content);
24+
}
25+
26+
static async Task<bool> WriteToFileAsync(IStorageFile file, Stream content)
27+
{
28+
CachedFileManager.DeferUpdates(file);
29+
30+
await using var stream = await file.OpenStreamForWriteAsync();
31+
stream.SetLength(0);
32+
33+
await content.CopyToAsync(stream);
2434

25-
return true;
35+
var status = await CachedFileManager.CompleteUpdatesAsync(file);
36+
return status == Windows.Storage.Provider.FileUpdateStatus.Complete;
2637
}
2738

2839
static FileSavePicker? CreatePicker()

0 commit comments

Comments
 (0)