简体   繁体   中英

UWP/C# More efficient way of handling FileSavePicker?

Ive got the following FileSavePicker function running on button press. I was wondering if this is an acceptable way of handling the FileSave or if there are any better practices that i should be adopting when handling save files?

// Save File SubScriptHere:

private async void SaveButton_ClickAsync(object sender, RoutedEventArgs e)
{
    FileSavePicker savePicker = new FileSavePicker();
    savePicker.FileTypeChoices.Add("Text Document", new List<string> { ".txt" });
    savePicker.FileTypeChoices.Add("CSV Document", new List<string> { ".csv" });
    StorageFile file = await savePicker.PickSaveFileAsync();
}

It seems really basic and seems to run perfectly fine. Im just trying to adopt good habits where possible as im still learning. Should I be handling errors or confirming if the file saves correctly etc? It just seems too barebones for me to be comfortable with what ive come up with

I was wondering if this is an acceptable way of handling the FileSave ...

Yes, it certainly is. You need to remember to take care of the file, ie save it somewhere, though. The FileSavePicker just lets the user choose the file name, extension, and storage location for a file. You could use the FileIO.WriteTextAsync method to actually save it:

private async void SaveButton_ClickAsync(object sender, RoutedEventArgs e)
{
    FileSavePicker savePicker = new FileSavePicker();
    savePicker.FileTypeChoices.Add("Text Document", new List<string> { ".txt" });
    savePicker.FileTypeChoices.Add("CSV Document", new List<string> { ".csv" });
    StorageFile file = await savePicker.PickSaveFileAsync();
    if (file != null)
        await FileIO.WriteTextAsync(file, "contents...");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM