Learn how to store and retrieve settings and files from the roaming application data store.For info about the roaming application data store and why you'd want to use it, see Roaming application data.
Roadmap: How does this topic relate to others? See:
Register to receive notification when roaming data changes
Register for the DataChanged event. This example sets DataChangeHandler as the handler for roaming data changes.
void InitHandlers() { Windows.Storage.ApplicationData.Current.DataChanged += new TypedEventHandler<ApplicationData, object>(DataChangeHandler); } void DataChangeHandler(Windows.Storage.ApplicationData appData, object o) { // TODO: Refresh your data }
Get the containers for the app's settings and files
Use the ApplicationData.RoamingSettings property to get the settings and the ApplicationData.RoamingFolder property to get the files.
Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; Windows.Storage.ApplicationDataContainer roamingFolder = Windows.Storage.ApplicationData.Current.RoamingFolder;
Write data to a setting
Use the ApplicationDataContainer.Values property to access the settings in the roamingSettings container we got in the previous section. This example creates a setting named exampleSetting and a HighPriority setting that is best suited for transitioning time-critical information such as application state. .
// Simple setting roamingSettings.Values["exampleSetting"] = "Hello World"; // High Priority setting, for example, last page position in book reader app roamingSettings.values["HighPriority"] = "65";
An ApplicationDataCompositeValue object contains settings that must be accessed atomically. This example creates a composite setting named exampleCompositeSetting and adds it to the roamingSettings container.
// Composite setting Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue(); composite["intVal"] = 1; composite["strVal"] = "string"; roamingSettings.Values["exampleCompositeSetting"] = composite;
Call the ApplicationDataContainer.CreateContainer method to create a settings container. This example creates a settings container named exampleContainer and adds a setting named exampleSetting. The Always value from the ApplicationDataCreateDisposition enumeration indicates that the container should be created if it does not already exist.
// Setting in a container Windows.Storage.ApplicationDataContainer container = roamingSettings.CreateContainer("exampleContainer", Windows.Storage.ApplicationDataCreateDisposition.Always); if (roamingSettings.Containers.ContainsKey("exampleContainer")) { roamingSettings.Containers["exampleContainer"].Values["exampleSetting"] = "Hello World"; }
Read data from a setting
Use the ApplicationDataContainer.Values property to access the exampleSetting setting in the roamingSettings container.
Use the ApplicationDataContainer.Values property to access the exampleCompositeSetting setting in the roamingSettings container.
// Composite setting Windows.Storage.ApplicationDataCompositeValue composite = (Windows.Storage.ApplicationDataCompositeValue)roamingSettings.Values["exampleCompositeSetting"]; if (composite == null) { // No data } else { // Access data in composite["intVal"] and composite["strVal"] }
Use the ApplicationDataContainer.Values property to access the exampleSetting setting in the exampleContainer container.
// Setting in a container bool hasContainer = roamingSettings.Containers.ContainsKey("exampleContainer"); bool hasSetting = false; if (hasContainer) { hasSetting = roamingSettings.Containers["exampleContainer"].Values.ContainsKey("exampleSetting"); }
Write data to a file
Use the file APIs, such as Windows.Storage.StorageFolder.CreateFileAsync and Windows.Storage.FileIO.WriteTextAsync, to create and update a file in the roaming app data store. This example creates a file named dataFile.txt in the roamingFolder container and writes the current date and time to the file. The ReplaceExisting value from the CreationCollisionOption enumeration indicates that the file should be replaced if it already exists.
async void WriteTimestamp() { Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DatetimeFormatting.DateTimeFormatter("longtime"); StorageFile sampleFile = await roamingFolder.CreateFileAsync("dataFile.txt", CreateCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTime.Now)); }
Read data from a file
Use the file APIs, such as Windows.Storage.StorageFolder.GetFileAsync, Windows.Storage.StorageFile.GetFileFromApplicationUriAsync and Windows.Storage.FileIO.ReadTextAsync to open and read a file in the roaming app data store. This example opens the dataFile.txt file created in the previous section and reads the date from the file. For details on loading file resources from various locations see How to load file resources.
async void ReadTimestamp() { try { StorageFile sampleFile = await roamingFolder.GetFileAsync("dataFile.txt"); String timestamp = await FileIO.ReadTextAsync(sampleFile); // Data is contained in timestamp } catch (Exception) { // Timestamp not found } }
Delete settings when finished with them
Call the ApplicationDataContainerSettings.Remove method to delete the exampleSetting setting from the roamingSettings container when you have finished with it.
Call the ApplicationDataCompositeValue.Remove method to delete the exampleCompositeSetting composite setting from the roamingSettings container when you have finished with it.
Call the ApplicationDataContainer.DeleteContainer method to delete the exampleContainer settings container when you have finished with it.
Remarks
Each app has a quota for roaming application data. Check the ApplicationData.RoamingStorageQuota property to determine the total size of roaming data allowed.
Related topics
- Tasks
- How to load file resources
- Quickstart: Local application data
- Quickstart: Temporary application data
- Conceptual
- Application data
- Guidelines
- Guidelines for roaming application data
- Reference
- Windows.Storage.ApplicationData
- Windows.Storage.ApplicationDataCompositeValue
- Windows.Storage.ApplicationDataContainer
- Windows.Storage.ApplicationDataContainerSettings
- Samples
- Application data sample
Build date: 10/26/2012