WindowsAppSDK icon indicating copy to clipboard operation
WindowsAppSDK copied to clipboard

FileSavePicker auto creates empty file after clicking OK button

Open d2phap opened this issue 1 month ago • 8 comments

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

  1. Run the code above
  2. When the File Save dialog appear, enter non-existing name, such as aaa.txt
  3. Click OK
  4. File aaa.txt is created!!
Image

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

d2phap avatar Nov 04 '25 14:11 d2phap

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 avatar Nov 04 '25 22:11 aepot

@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

d2phap avatar Nov 05 '25 03:11 d2phap

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.

DinahK-2SO avatar Nov 17 '25 07:11 DinahK-2SO

Hi @DinahK-2SO, thanks for looking into this issue.

I am migrating a WinForms photo viewer to WinUI 3, my save flow is:

  1. Users rotate an image, then click Save button
  2. Open FileSavePicker, let them to choose where to save
  3. Show a warning message if they're about to override to an existing file
  4. When they confirm, write pixel data to the file

With the current AppSdk behavior, there are some issues:

  1. There's no way to disable the default overwrite prompt (WinForms has SaveFileDialog.OverwritePrompt option)
  2. 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 FileSavePicker auto-creates an empty destination file, that safety model is undermined and the original file can be left vulnerable.

d2phap avatar Nov 17 '25 07:11 d2phap

Hi @d2phap ,

Thanks for sharing your insights! I'd love to discuss a couple of possible improvements:

  1. Could you share why the default warning prompt should be disabled? Is there any important information that the current prompt is missing? Image

  2. 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?

DinahK-2SO avatar Nov 17 '25 07:11 DinahK-2SO

  1. 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...

Image
  1. 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.

d2phap avatar Nov 17 '25 07:11 d2phap

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.

DinahK-2SO avatar Dec 04 '25 06:12 DinahK-2SO

That's great news! @DinahK-2SO Looking forward to the new version

d2phap avatar Dec 04 '25 06:12 d2phap