public static async Task<RecipeBox> CreateNewRecipeBoxAsync() { var savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; // Dropdown of file types the user can save the file as savePicker.FileTypeChoices.Add("Recipe Box", new List<string>() { ".rcpbx" }); // Default file name if the user does not type one in or select a file to replace savePicker.SuggestedFileName = "New Document"; Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { // Prevent updates to the remote version of the file until // we finish making changes and call CompleteUpdatesAsync. Windows.Storage.CachedFileManager.DeferUpdates(file); // write blank RecipeBox to file RecipeBox rb = new RecipeBox(file.Name); rb.LastPath = file.Path; string rbJson = JsonConvert.SerializeObject(rb); await Windows.Storage.FileIO.WriteTextAsync(file, rbJson); // Let Windows know that we're finished changing the file so // the other app can update the remote version of the file. // Completing updates may require Windows to ask for user input. Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) { return rb; } else { return null; } } else { //__could insert code here for case of picker cancelled return null; } }
public static async Task SaveRecipeBox(RecipeBox recipeBox) { string path = recipeBox.LastPath; try { string recipeBoxJson = JsonConvert.SerializeObject(recipeBox); StorageFile file = await StorageFile.GetFileFromPathAsync(path); await FileIO.WriteTextAsync(file, recipeBoxJson); return; } catch (Exception) { ///todo: have user pick new lolcation/name } }