Best way to save settings for a WinRT application?

I am working on a WinRT application, which is also a game. I need to store other information, such as sound settings or player statistics somewhere in the form of a file or in some way. If this is a file, just write the settings in ...? I have an idea, but I think this is too rudimentary ... What is the best way to get this?

Any help or suggestions are welcome!

+5
source share
2 answers

Here are some ways to save data in a WinRT application, the method with the settings in the name is probably what you are looking for! - others just added, - you can also serialize the data if you want. This is working code, but be sure to add error handling, etc. This is a simple demo code :)

As for settings, you can save simple settings as a key and values, and for more complex settings you can use a container. I gave both examples: =)

 public class StorageExamples
{
    public async Task<string> ReadTextFileAsync(string path)
    {
        var folder = ApplicationData.Current.LocalFolder;
        var file = await folder.GetFileAsync(path);
        return await FileIO.ReadTextAsync(file);
    }

    public async void WriteTotextFileAsync(string fileName, string contents)
    {
        var folder = ApplicationData.Current.LocalFolder;
        var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
        await FileIO.WriteTextAsync(file, contents);
    }

    public void SaveSettings(string key, string contents)
    {
        ApplicationData.Current.LocalSettings.Values[key] = contents;
    }

    public string LoadSettings(string key)
    {
        var settings = ApplicationData.Current.LocalSettings;
        return settings.Values[key].ToString();
    }
    public void SaveSettingsInContainer(string user, string key, string contents)
    {
        var localSetting = ApplicationData.Current.LocalSettings;

        localSetting.CreateContainer(user, ApplicationDataCreateDisposition.Always);

        if (localSetting.Containers.ContainsKey(user))
        {
            localSetting.Containers[user].Values[key] = contents;
        }
    }
}
+16
source

The MSDN article is about using application settings in Windows Store apps .

The namespace Windows.UI.ApplicationSettingscontains all the necessary classes.

, , Windows. .

. , .

+3

All Articles