Sdílet prostřednictvím


Quickstart: Temporary app data (XAML)

[ 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 ]

Learn how to store and retrieve files from the temporary app data store.

Roadmap: How does this topic relate to others? See:

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.

Windows.Storage.StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
Dim temporaryFolder As Windows.Storage.StorageFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder
StorageFolder^ temporaryFolder = ApplicationData::Current->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.

async void WriteTimestamp()
{
   Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = 
       new Windows.Globalization.DatetimeFormatting.DateTimeFormatter("longtime");

   StorageFile sampleFile = await temporaryFolder.CreateFileAsync("dataFile.txt", 
       CreateCollisionOption.ReplaceExisting);
   await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTime.Now));
}
Imports Windows.Globalization.DateTimeFormatting

Private Async Sub WriteTimestamp()
   Dim formatter As DateTimeFormatter = New DateTimeFormatter("longtime")

   Dim sampleFile As StorageFile = Await temporaryFolder.CreateFileAsync("dataFile.txt", 
       CreationCollisionOption.ReplaceExisting)
   Await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTime.Now));
End Sub
void MainPage::WriteTimestamp()
{
   concurrency::task<StorageFile^> fileOperation = 
       temporaryFolder->CreateFileAsync("dataFile.txt", CreateCollisionOption::ReplaceExisting);
   fileOperation.then([this](StorageFile^ sampleFile)
   {
      auto calendar = ref new Calendar;
      auto now = calendar->ToDateTime();
      auto formatter = ref new Windows::Globalization::DateTimeFormatting::DateTimeFormatter("longtime");

      return FileIO::WriteTextAsync(sampleFile, formatter->Format(now));
   }).then([this](task<void> previousOperation) {
      try {
         previousOperation.get();
      } catch (Platform::Exception^) {
         // Timestamp not written
      }
   });
}

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. For details on loading file resources from various locations see How to load file resources.

async void ReadTimestamp()
{
   try
   {
      StorageFile sampleFile = await temporaryFolder.GetFileAsync("dataFile.txt");
      String timestamp = await FileIO.ReadTextAsync(sampleFile);
      // Data is contained in timestamp
   }
   catch (Exception)
   {
      // Timestamp not found
   }
}
Private Async Function ReadTimestamp() As Task
   Try
      Dim sampleFile As StorageFile = Await temporaryFolder.GetFileAsync("dataFile.txt")
      Dim timestamp As string = Await FileIO.ReadTtextAsync(sampleFile)
      ' Data is contained in timestamp
   Catch e1 As Exception
      ' Timestamp not found
   End Try
End Function
void MainPage::ReadTimestamp()
{
   concurrency::task<StorageFile^> getFileOperation(temporaryFolder->GetFileAsync("dataFile.txt"));
   getFileOperation.then([this](StorageFile^ file)
   {
      return FileIO::ReadTextAsync(file);
   }).then([this](concurrency::task<String^> previousOperation) {
      String^ timestamp;
 
      try {
         timestamp = previousOperation.get();
         // Data is contained in timestamp
      } catch (...) {
         // Timestamp not found
      }
   });
}

Tasks

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