WinRT Roaming Settings

One thing consider when developing apps is that we might need to have data persist between uses or even between devices. With Windows 8 we are given 3 places where we can store data for each user profile (apart from the system libraries):

Local: The local folder is just a basic, local folder for you to dump data to. Nothing special here. Good to store some settings for you app, for example.

Temp: The Temp folder is for temporary data… obviously. You can store data here but at your own risk since it might get deleted.

Roaming: This is a very cool little folder. Whatever you store in the Roaming folder, like the name suggests, automatically roams across all devices. So if a user has your app in all his devices you can have his personal settings synced in all instances of your app. The one thing you must pay attention to is that this folder has a size limit of 100KB. And if you by any chance go over that limit, nothing(!) will roam.

Storing and Retrieving Data

Storing data is quite easy. Everything you need is in the Windows.Storage.ApplicationData.Current class. You have acces to LocalFolder, LocalSettings, RoamingFolder, RoamingSettings, RoamingSettingsQuota (remember, 100KB limit) and TemporaryFolder. in order to actually write something, say in the RoamingSettings, you can the following:

var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
var composite = new Windows.Storage.ApplicationDataCompositeValue();

composite["setting1"] = ...;
composite["setting2"] = ...;

roamingSettings.Values["mySettings"] = composite;

We get a reference to the RoamingSettings folder, stick our data into a container, and add the container to the settings. Getting our data back is just as easy:

var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
var composite = (Windows.Storage.ApplicationDataCompositeValue)roamingSettings.Values["mySettings"];

//if null, then there's nothing in the Roaming folder
if (composite == null)
{
    ...
}
else
{
    MySetting1 = composite["setting1"].ToString();
    MySetting2 = composite["setting2"].ToString();
}

So, there. Simple and easy!

Filipe Duarte

Filipe Duarte
Hi, I’m Filipe. I’m a Software Engineer, from Portugal, currently living in London, building stuff for Paddle.

Build 2020 Highlights

Build has started today, and like most events during this time it has gone virtual. I've been following it online and just wanted to ment...… Continue reading

2019 Year in Review

Published on January 01, 2019

Windows 10 App - Zero Flip

Published on December 23, 2017