FileSavePicker auto creates empty file after clicking OK button
Describe the bug
The FileSavePicker creates an empty file without code.
var saveDialog = new FileSavePicker(AppWindow.Id)
{
DefaultFileExtension = ".xml",
};
saveDialog.FileTypeChoices.TryAdd("TXT", [".txt"]);
saveDialog.FileTypeChoices.TryAdd("JSON", [".json"]);
saveDialog.FileTypeChoices.TryAdd("XML", [".xml"]);
var picker = await saveDialog.PickSaveFileAsync();
// that's it, do nothing here
Steps to reproduce the bug
- Run the code above
- When the File Save dialog appear, enter non-existing name, such as
aaa.txt - Click OK
- File
aaa.txtis created!!
Expected behavior
The file is not created automatically, the FileSavePicker should only return a full path.
Screenshots
No response
NuGet package version
Windows App SDK 1.8.2: 1.8.251003001
Packaging type
Unpackaged
Windows version
Windows 11 version 24H2 LTSC (26100, June Update)
IDE
Visual Studio 2022
Additional context
No response
AFAIK, that's an expected default behavior. In Windows Forms or WPF the dialog does the same.
But at least in WPF this can be opt-out by SaveFileDialog.CreateTestFile Property.
@aepot I'm not sure about WPF, but in WinForms, it does not happen (as I'm porting my app from WinForms to WinUI3).
Checking the docs, there is a similar property SaveFileDialog.CheckWriteAccess, I have never tried this before, but I guess if I set it to true, it may do the same as WPF's CreateTestFile.
The problem here is there is no option to control this behavior, as well as SaveFileDialog.OverwritePrompt
Hi @d2phap ,
Thank you for your feedback! The default behavior of UWP’s FileSavePicker is to create an empty file when the PickSaveFileAsync() method was called, so the new storage picker follows the same approach.
Does this behavior have any impact on your development workflow? Or would your app benefit from having the option to customize whether an empty file is created? Please let us know your thoughts.
Really appreciate your insight.
Hi @DinahK-2SO, thanks for looking into this issue.
I am migrating a WinForms photo viewer to WinUI 3, my save flow is:
- Users rotate an image, then click Save button
- Open FileSavePicker, let them to choose where to save
- Show a warning message if they're about to override to an existing file
- When they confirm, write pixel data to the file
With the current AppSdk behavior, there are some issues:
- There's no way to disable the default overwrite prompt (WinForms has SaveFileDialog.OverwritePrompt option)
- Risk of data loss on error: my approach writes to a temp file and only replaces the original after success (critical for a photo editor!). When
FileSavePickerauto-creates an empty destination file, that safety model is undermined and the original file can be left vulnerable.
Hi @d2phap ,
Thanks for sharing your insights! I'd love to discuss a couple of possible improvements:
-
Could you share why the default warning prompt should be disabled? Is there any important information that the current prompt is missing?
-
for the save picker's behavior, if we change it to:
- only creates an empty file when the picked file does not exist
- and does not override the original file if the picked file already exists
would that change address the requirements for safety in your app?
- Could you share why the default warning prompt should be disabled? Is there any important information that the current prompt is missing?
I used to use the default overwrite prompt, but my users always skip it as it's not outstanding, and I want to show more insights for the warning message. Here is an example:
After users click "OK" button from the FileSavePicker, I show this warning to emphasizes the image metadata may be lost...
- for the save picker's behavior, if we change it to:
- only creates an empty file when the picked file does not exist
- and does not override the original file if the picked file already exists
would that change address the requirements for safety in your app?
This would address the safety issue but still less flexible. My suggestion:
- Keep the current behavior for backward compatibility
- Introduce a new option
CreateTestFile=false(similar to WPF's SaveFileDialog.CreateTestFile).- If
CreateTestFile=true, keep the current behavior or implement the above proposal - If
CreateTestFile=false, just let users handle the file creation, don't auto-create it.
- If
Hi @d2phap, thank you for raising these points and sharing your perspective
As a first step, we’ve updated the FileSavePicker to check if the selected file already exists, and ensured it won’t truncate an existing file. This change will be available from versions 2.0-exp4 and 1.8.4.
With this change, your app can display the outstanding dialog for user consent before actually overwriting any files.
Regarding the rest 2 requests: hide the warning prompt, and disable file creation, they involve new API contracts and potential behavior changes, so may need further investigation. I'll update here as we explore these enhancements.
Thanks again for your valuable input! Your insights help us make meaningful improvements, and we look forward to continuing this collaboration.
That's great news! @DinahK-2SO Looking forward to the new version