Dela via


Quickstart: Local app data (HTML)

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

Applies to Windows and Windows Phone

Learn about storing and retrieving settings and files from the local app data store.

Get the containers for the app's settings and files

Use the ApplicationData.localSettings property to get the settings in an ApplicationDataContainer object. Use the ApplicationData.localFolder property to get the files in a StorageFolder object.

var localSettings = applicationData.localSettings;
var localFolder = applicationData.localFolder;

The next steps use the localSettings and localFolder variables from this step.

Write data to a setting

Use the ApplicationDataContainer.values property to access the settings in the localSettings container we got in the previous step. This example creates a setting named exampleSetting.

// Simple setting

localSettings.values["exampleSetting"] = "Hello Windows";

An ApplicationDataCompositeValue object contains settings that must be accessed atomically. This example creates a composite setting named exampleCompositeSetting and adds it to the localSettings container.

// Composite setting

var composite = new Windows.Storage.ApplicationDataCompositeValue();
composite["intVal"] = 1;
composite["strVal"] = "string";

localSettings.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 is created if it doesn't already exist.

// Setting in a container

var container = localSettings.createContainer("exampleContainer", Windows.Storage.ApplicationDataCreateDisposition.always);

if (localSettings.containers.hasKey("exampleContainer"))
{
    localSettings.containers.lookup("exampleContainer").values["exampleSetting"] = "Hello Windows";
}

Read data from a setting

Use the ApplicationDataContainer.values property to access the exampleSetting setting in the localSettings container.

// Simple setting

var value = localSettings.values["exampleSetting"];
        
if (!value)
{
    // No data
}
else
{
    // Access data in value
}

Use the ApplicationDataContainer.values property to access the exampleCompositeSetting setting in the localSettings container.

// Composite setting

var composite = localSettings.values["exampleCompositeSetting"];

if (!composite)
{
    // 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

var hasContainer = localSettings.containers.hasKey("exampleContainer");

if (hasContainer)
{
    // Access data in localSettings.containers.lookup("exampleContainer").values.hasKey("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 local app data store. This example creates a file named dataFile.txt in the localFolder container and writes the current date and time to the file. The replaceExisting value from the CreationCollisionOption enumeration indicates to replace the file if it already exists.

function writeTimestamp() {
   localFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
      .then(function (sampleFile) {
         var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
         var timestamp = formatter.format(new Date());

         return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp);
      }).done(function () {      
      });
}

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 local app data store. This example opens the dataFile.txt file created in the previous step and reads the date from the file. The openIfExists value from the CreationCollisionOption enumeration indicates that the file must exist. For details on loading file resources from various locations, see How to load file resources.

function readTimestamp() {
   localFolder.getFileAsync("dataFile.txt")
      .then(function (sampleFile) {
         return Windows.Storage.FileIO.readTextAsync(sampleFile);
      }).done(function (timestamp) {
         // Data is contained in timestamp
      }, function () {
         // Timestamp not found
      });
}

Delete settings when finished with them

Call the ApplicationDataContainerSettings.remove method to delete the exampleSetting setting when you have finished with it.

// Simple setting

localSettings.values.remove("exampleSetting");

Call the ApplicationDataCompositeValue.remove method to delete the exampleCompositeSetting composite setting when you have finished with it.

// Delete composite setting

localSettings.values.remove("exampleCompositeSetting");

Call the ApplicationDataContainer.deleteContainer method to delete the exampleContainer settings container when you have finished with it.

// Delete container

localSettings.deleteContainer("exampleContainer");

Task

How to load file resources

Quickstart: Roaming app data

Quickstart: Temporary app data

Conceptual

Accessing app data with the Windows Runtime

Reference

Windows.Storage.ApplicationData

Windows.Storage.ApplicationDataCompositeValue

Windows.Storage.ApplicationDataContainer

Windows.Storage.ApplicationDataContainerSettings

Samples

Application data sample