How to save a file of user-defined format/extension?

帕菲菲 141 Reputation points
2021-07-20T06:40:46.103+00:00

My App allows the user to directly operate on the raw binary of a file, so the user may want to save the file with any extension. However, FileSavePicker does not allow a wildcard extension.
Currently, I have to show a flyout containing a textbox, requiring the user to type the extension to save as. But this behavior I feel is somewhat strange.
Any better workaround?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2021-07-20T08:52:16.197+00:00

    Hello, Welcome to Micorosoft Q&A,

    How to save a file of user-defined format/extension?

    As you mentioned above, FileSavePicker does not allow a wildcard extension, for this scenario, the better way is launch the FileSavePicker with extension programmatic, you could package the FileSavePicker with extension parameter.

    For example

    private async Task<StorageFile> PickFileAsync(string extension)  
    {  
        var picker = new FileSavePicker();  
        picker.SuggestedStartLocation = PickerLocationId.Downloads;  
        picker.SuggestedFileName = "saved file";  
        picker.DefaultFileExtension = ".txt";  
        picker.FileTypeChoices.Add("file", new List<string> { extension });  
      
        var file = await picker.PickSaveFileAsync();  
        return file;  
    }  
    

    Usage

    var file =  await PickFileAsync(".pp");  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.