Quickstart: Temporary app data (HTML)

Applies to Windows and Windows Phone

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

Get the container for the app's files

Use the ApplicationData.temporaryFolder property to get the files. The next steps use the temporaryFolder variable from this step.



var applicationData = Windows.Storage.ApplicationData.current;
var temporaryFolder = applicationData.temporaryFolder;

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 temporary app data store. This example creates a file named dataFile.txt in the temporaryFolder 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() {
   temporaryFolder.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 temporary 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() {
   temporaryFolder.getFileAsync("dataFile.txt")
      .then(function (sampleFile) {
         return Windows.Storage.FileIO.readTextAsync(sampleFile);
      }).done(function (timestamp) {
         // Data is contained in timestamp
      }, function () {
         // Timestamp not found
      });
}

Related topics

Task
How to load file resources
Quickstart: Local app data
Quickstart: Roaming 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

 

 

Show:
© 2014 Microsoft