Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Windows 8 App - Data storage in files using Windows.Storage namespace

0.00/5 (No votes)
10 May 2013 1  
Learn to store/read data from Windows 8 app and available file storage options for Win8 apps.

Introduction

Win 8 App can store data into 3 folders using these classes under Windows.Storage namespace. 

  • Windows.Storage.ApplicationData.current.localFolder
  • Windows.Storage.ApplicationData.current.roamingFolder
  • Windows.Storage.ApplicationData.current.temporaryFolder

Local Folder 

It stores files under LocalState folder under App Package. These files are not synchronized on all machines the user signed in with same account like Window Phone, Tablet or PC and it remain on the machine on which the file was originally created.

Roaming Folder  

It stores files under RoamingState folder under App Package. These files are synchronized on all machines the user signed in with same account like Window Phone, Tablet or PC. But the synchronization will not be instant. It will depend on internet access on these devices or other factors like the device not ready to send/receive data. Roaming data should be less than the available quota (check RoamingStorageQuota property), or else the App will not able the synchronize data under RoamingState folder. Files will be synchronized when the application closed/saved the file after editing.

Temporary Folder   

It stores files under TempState folder under App Package. The files under this folder can be deleted when these are not in use or depends on available disk capacity and the age of a file. Do not use this folder to store important information from user.   

All these are be located under the App package folder.

This code example will create a sampleFile.txt under the RoamingState folder.

StorageFile

Source Code

MainPage.xaml

Win8App_Screen 

<Page x:class="TestApp.MainPage" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="using:TestApp" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      mc:ignorable="d">

  <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock x:Name="TextBlock1" HorizontalAlignment="Left" 
           Height="40" Margin="41,127,0,0" TextWrapping="Wrap" 
           Text="TextBlock" VerticalAlignment="Top" Width="201" 
           FontSize="36"/>
        <Button Content="Read" HorizontalAlignment="Left" 
           Height="40" Margin="394,54,0,0" VerticalAlignment="Top" 
           Width="134" Click="Read_Button_Click"/>
        <TextBox x:Name="TextBox1" HorizontalAlignment="Left" 
           Height="33" Margin="41,61,0,0" TextWrapping="Wrap" 
           Text="TextBox" VerticalAlignment="Top" Width="168"/>
        <Button Content="Write" HorizontalAlignment="Left" 
           Height="40" Margin="240,54,0,0" 
           VerticalAlignment="Top" Width="134" 
           Click="Write_Button_Click"/>
  </Grid>
</Page>

MainPage.xaml.cs ( with Roaming Storage)

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Storage;

namespace TestApp
{
    
    public sealed partial class MainPage : Page
    {
        StorageFolder StorageFolder = null;
        const string filename = "sampleFile.txt";

        public MainPage()
        {
            this.InitializeComponent();
            StorageFolder = ApplicationData.Current.RoamingFolder;
        }

        private void Read_Button_Click(object sender, RoutedEventArgs e)
        {
            ReadText();
        }

        private void Write_Button_Click(object sender, RoutedEventArgs e)
        {
            WriteText();
        }

        async void ReadText()
        {
            StorageFile file = await StorageFolder.GetFileAsync(filename);
            TextBlock1.Text  = await FileIO.ReadTextAsync(file);
        }
        async void WriteText()
        {
            StorageFile file = await StorageFolder.CreateFileAsync(filename, 
                                       CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteTextAsync(file, TextBox1.Text);
        }
    }
}

For local and temporary storage choose different folder options from ApplicationData.Current.

StorageOptions 

Local Storage

public MainPage()
{
    this.InitializeComponent();
    StorageFolder = ApplicationData.Current.LocalFolder;
}

Temporary Storage 

public MainPage()
{
    this.InitializeComponent();
    StorageFolder = ApplicationData.Current.TemporaryFolder;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here