Application Data in Universal Windows Apps

Application data is state than an app manages within its app data folders. Local, Roaming and Temporary along with LocalCache are the different data folders for Windows Phone 8.1 apps. The existence of app data is tied to the existence of the app so that uninstalling the app deletes the app data from that device. Roaming app data persists in the cloud independently, and is shared between Windows and Windows Phone apps that share the same Package Family name in their manifests.
App data is preserved across app updates delivered by the Windows Store and Windows Phone Store. So app updates must be prepared to load app data that was generated by any previous version of the app.

Folders

Local and LocalCache folders are both intended for storing device-specific app data. The difference is that Local app data is included with Windows Phone 8.1 backups, whereas LocalCache and Temporary are not. The difference between LocalCache and Temporary is that the system can delete temporary app data at any time to free up storage resources, whereas LocalCache data is under the app’s control. The LocalFolder, RoamingFolder, and TemporaryFolder properties (and LocalCacheFolder on Windows Phone 8.1) each map to discrete folders within the app’s root app data folder, which is located in %localappdata%packages{app_package_family_name}.

The LocalFolder of the app data can be accessed as follows:

var localFolder = ApplicationData.Current.LocalFolder;

Instead of targeting LocalFolder you could also target RoamingFolder or TemporaryFolder.

Version Numbers

The version number that you set by using Windows.ApplicationData.SetVersionAsync is independent from the version number of the app as set in its manifest. Any number of app versions can share the same app data version. When an app update needs to change the structure of the app data, it should call SetVersionAsync and provide a callback method to handle migration of an older version of its state. Subsequent updates can then continue to use that same app data version until such time as another change is needed.

The version of your app data primarily determines how different versions of roaming state are managed in the cloud. OneDrive maintains the most recent copy of each version of roaming app data that is in use by those versions of the app that the user has installed.
The version start at zero and can be obtain by checking

var version = ApplicationData.Current.Version;

If the version is lower than the expected to some converting and set the version to the correct one

ApplicationData.Current.SetVersionAsync(1, SetVersionHandler);

Saving Data

Saving data to local storage works as before.

var localFolder = ApplicationData.Current.LocalFolder;
var localFile = await localFolder.CreateFileAsync("localFile.txt", CreationCollisionOption.ReplaceExisting);
var fileBytes = System.Text.Encoding.UTF8.GetBytes("some text string");
using (var s = await localFile.OpenStreamForWriteAsync())
{
    s.Write(fileBytes, 0, fileBytes.Length);
}

Instead of targeting LocalFolder you could also target RoamingFolder or TemporaryFolder.

Another function which is very useful is the FileIO.WriteTextAsync. This makes it very easy to write text to files.

var tempFolder = ApplicationData.Current.TemporaryFolder;
var tempFile = await roamingFolder.CreateFileAsync("tempFile.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(tempFile, "some text string");

TemporaryFolder

This is the place where you can save data without thinking of have to remove it later. TemporaryFolder can be used for example for sharing data between several views when you for some reason don´t want to keep the data in memory. You can save images to the temporary folder for use in your views if you don´t want to persist them in your local folder. From XAML you can target the TemporaryFolder by using ms-appdata:///temp/.

RoamingFolder

If you save data to the RoamingFolder the data is available on every device the app is installed. If you want to find out when roaming data has been changed by someone else you need to listen to DataChanged. This fires if roaming data is change by another app:

applicationData.DataChanged += DataChangedHandler;

private async void DataChangedHandler(ApplicationData appData, object o)
{
    // Add code
}

RoamingStorageQuota

By calling RoamingStorageQuota you get the amount of data possible to roam. If you try to roam more data than RoamingStorageQuota specifies the system stop replication the data until it below the limit again. The normal amount of data possible to roam is 100kb.

var quota = applicationData.RoamingStorageQuota;

Settings

Instead of targeting LocalStorage it is possible to target LocalSettings, there is also a RoamingSettings. This works the same as folders does, local saves locally and roaming saves to the cloud. Its quite easy to save settings.

var roamingSettings = ApplicationData.Current.RoamingSettings;
roamingSettings.Values["MySetting"] = "Hello World";

In addition to save text to a setting one can save a composite value. This is a setting which contains several settings:

var composite = new ApplicationDataCompositeValue();
composite[settingName1] = 1;
composite[settingName2] = "world";
roamingSettings.Values["MyCompositeSetting"] = composite;

It is also possible to create containers in the settings. This for making it easier to structure the settings. Example of a container:

var container = localSettings.CreateContainer("exampleContainer", ApplicationDataCreateDisposition.Always);
if (localSettings.Containers.ContainsKey("exampleContainer"))
{
    localSettings.Containers["MyContainer"].Values["MySetting"] = "Hello Windows";
}

Vivek Maskara

I am pursuing BTech in Software Engineering from Delhi Technological University. I develop apps, create websites and blog about my app experiences.

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding
Close