none
[UWP]How should I save a list in a UWP app? RRS feed

  • Question

  • I would like to save the contents of a list in C# when the user closes the application.  This seems rather easy to do with a Streamwriter; however, I don't believe I can do that within a UWP app. So, how should I save the contents of a list in a UWP app using the methods available for the Windows platform?
    Monday, August 12, 2019 8:55 PM

All replies

  • If I could provide some feedback (although there's much more highly qualified on here than I) - the Dev Center has some good documentation on file storage here -> https://docs.microsoft.com/en-us/windows/uwp/design/app-settings/store-and-retrieve-app-data

    It would depend on the use of the application and the frameworks you're using, so more feedback from you would be helpful on the use of the application - such as multiple users where a database would be needed, or one user storing local app data such as settings, etc.

    If the list is being saved by only one user at any given time - I would suggest a text file, SQLite local db, or the above links' uses of technologies, where conflicting copies and overwrites wouldn't be an issue.

    On the other hand - if the list is to be accessed and saved by multiple users at any given time - a SQL server db would work well. 

    There are honestly so many ways to solve data storage problems. In order to provide specific examples I think more information about the specific uses of the data would be helpful.

    Monday, August 12, 2019 9:54 PM
  • Hi,

    If you want to save the list data, you can use serialized storage, such as saving the list as JSON.

    Here is the example (Before that, you need to install the Newtonsoft.Json nuget package.):

    var stringList = new List<string>();
    
    // Todo: Data init
    
    string serializeText = JsonConvert.SerializeObject(stringList);
    
    var localFolder = ApplicationData.Current.LocalFolder;
    var localFile = await localFolder.CreateFileAsync("saveList.json", CreationCollisionOption.OpenIfExists);
    
    await FileIO.WriteTextAsync(localFile, serializeText);
    


    When you want to read serialized text and convert it to a list, you can use the following code

    var localFolder = ApplicationData.Current.LocalFolder;
    var localFile = await localFolder.CreateFileAsync("saveList.json", CreationCollisionOption.OpenIfExists);
    
    string localText = await FileIO.ReadTextAsync(localFile);
    
    var list = JsonConvert.DeserializeObject<List<string>>(localText);
    

    If you have a lot of data, I suggest you use the database.

    Best regards.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 13, 2019 1:57 AM