Quickstart: Temporary application data (Windows Store apps using JavaScript and HTML)

Language: JavaScript and HTML | VB/C#/C++ and XAML
0 out of 3 rated this helpful - Rate this topic

Learn about storing and retrieving files from the temporary application 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 that the file should be replaced 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 application data
Quickstart: Roaming application data
Conceptual
Application data
Reference
Windows.Storage.ApplicationData
Windows.Storage.ApplicationDataCompositeValue
Windows.Storage.ApplicationDataContainer
Windows.Storage.ApplicationDataContainerSettings
Samples
Application data sample

 

 

Build date: 11/29/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.