Search code examples
c#filewindows-runtimewindows-store-appsfilesavepicker

Offering multiple fileTypes with the same extension through FileSavePicker


In my WinRT / Windows Store App I want to allow the user to save a file as either plain text or markdown text through a FileSavePicker.

savePicker.FileTypeChoices.Add("Markdown Text", new List<string>() { ".txt" });
savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });

However, the returned object from FileSavePicker is a StorageFile, and all it knows is the extension, .txt.

How do I allow the user to choose between a bunch of options with the same file extension? Is there a way to do this?


Solution

  • This is not directly supported, because the system only recognizes each file extension as a single type. You do have a few options though.

    • Create a different file type for each file type
    • Create a different file type for each file type, then rename the file to .txt after they've selected it.

    Ex:

    savePicker.FileTypeChoices.Add("Markdown Text", new List<string>() { ".mtxt" });
    savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".ptxt" });
    
    var file = await savePicker.PickSaveFileAsync();
    await file.RenameAsync(file.DisplayName + ".txt");
    

    This has the danger that it will not warn them if they have a file of that name already there though, so is not necessarily suggested.

    • Have a checkbox/drop down next to the button you use to save the file that gives the option.

    Of these, the first or the third are the most ideal. If there is a difference between them, it would be good to differentiate it in the file system. If that is still not desired, then giving them the option before they select to save is the best option.

    Edit: @Krekkon's idea is good as well. Adding the option to a settings flyout is a good idea. You'll need to inform them that the option exists though. The other option is to use something like a MessageDialog which popups up three buttons: Save as Plain Text, Save as Markup, Cancel. This will unfortunately add a click to the flow, so may not be ideal. Personally I think adding a simple control next to the save button is simple and fine.