none
UWP Save file in local app folder when user exits RRS feed

  • Question

  • I am making an application that will save a file from an array when the user exits. I have placed the following code at the bottom of the app.xaml.cs page where the exit event handler is normally, however, this code is not properly saving my file.

    private async void OnSuspending(object sender, SuspendingEventArgs e) { await Closer(); var deferral = e.SuspendingOperation.GetDeferral(); //TODO: Save application state and stop any background activity deferral.Complete(); } private async Task Closer() { Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; Windows.Storage.StorageFile saver = await storageFolder.CreateFileAsync("scores.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting); var listOfStrings = new List<string> { Globals.scoreKeeper[0], Globals.scoreKeeper[1], Globals.scoreKeeper[2], Globals.scoreKeeper[3], Globals.scoreKeeper[4], Globals.scoreKeeper[5], Globals.scoreKeeper[6], Globals.scoreKeeper[7], Globals.scoreKeeper[8], Globals.scoreKeeper[9], Globals.scoreKeeper[10], Globals.scoreKeeper[11], Globals.scoreKeeper[12], Globals.scoreKeeper[13], Globals.scoreKeeper[14], Globals.scoreKeeper[15], Globals.scoreKeeper[16], Globals.scoreKeeper[17], Globals.scoreKeeper[18], Globals.scoreKeeper[19], Globals.scoreKeeper[20], Globals.scoreKeeper[21], Globals.scoreKeeper[22], Globals.scoreKeeper[23], Globals.scoreKeeper[24], Globals.scoreKeeper[25], Globals.scoreKeeper[26], Globals.scoreKeeper[27] }; await Windows.Storage.FileIO.AppendLinesAsync(saver, listOfStrings); }

    This same save system does work on other parts of the application, but it wipes the whole file every time I run it here. I have thoroughly searched multiple programming help websites, but I have found no solution to this problem. If possible, I would appreciate a prompt response.

    Thanks.

    Saturday, January 5, 2019 5:51 PM

Answers

  • maybe something like....

    private async System.Threading.Tasks.Task SaveAsync() 
            {
                Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                Windows.Storage.StorageFile LinkFile = await storageFolder.GetFileAsync("scores.txt");
                await Windows.Storage.FileIO.WriteLinesAsync(LinkFile, listOfStrings );
            }


    • Edited by ThisNewbie Saturday, January 5, 2019 6:33 PM
    • Marked as answer by Zaa3015 Saturday, January 5, 2019 7:01 PM
    Saturday, January 5, 2019 6:23 PM

All replies

  • maybe something like....

    private async System.Threading.Tasks.Task SaveAsync() 
            {
                Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                Windows.Storage.StorageFile LinkFile = await storageFolder.GetFileAsync("scores.txt");
                await Windows.Storage.FileIO.WriteLinesAsync(LinkFile, listOfStrings );
            }


    • Edited by ThisNewbie Saturday, January 5, 2019 6:33 PM
    • Marked as answer by Zaa3015 Saturday, January 5, 2019 7:01 PM
    Saturday, January 5, 2019 6:23 PM
  • Thank you so much! This solves my problem.
    Saturday, January 5, 2019 7:01 PM