Roaming and Local Settings in UWP

May 06, 2016

In the new Universal Windows Platform, you can store settings data in many ways. These days, there are multiple cloud options that you can communicate with; alternatively, you can store the settings in the user’s local profile. You can do this locally (that is, the data is stored on the current device against the current user only), or remotely (which means that the data will automatically be shared across all devices where your app is installed for that user). Both of these are stored as KeyValue Pairs.

Local Settings

Local settings are small, local variables. These are accessible even offline. The following code will only work on VS2015 because of the ”?.” syntax; however, a null coalesce ”??” will work just as well in older versions of VS.



string mySetting = Windows.Storage.ApplicationData.Current.LocalSettings.Values["MySetting"]?.ToString();

To set the setting, the syntax is basically the same:



Windows.Storage.ApplicationData.Current.LocalSettings.Values["MySetting"] = "1";

There is, as far as I’m aware, no limit to the amount of data that can be stored in these settings.

Roaming Settings

Roaming settings are a small, out of the box, solution for transferring settings across Windows devices. They aren’t new, and they are very easy to use; the following will return “MySetting” or null (VS2015+ only):



string mySetting = Windows.Storage.ApplicationData.Current.RoamingSettings.Values["MySetting"]?.ToString();

To set the setting, the syntax is basically the same:



Windows.Storage.ApplicationData.Current.RoamingSettings.Values["MySetting"] = "1";

The amount of data that can be stored here is apparently, around 100K. You can get the exact figure by interrogating the RoamingStorageQuota property:



Int storage = Windows.Storage.ApplicationData.Current.RoamingStorageQuota

Take this limit seriously; this article implies that, should you exceed it, all roaming will be stopped.

It’s also worth bearing in mind that you need to be online to access roaming storage.



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024