Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / C# 5.0
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
10 May 2013CPOL1 min read 32.7K   254   2   3
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.

Image 1

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

StorageFile

Source Code

MainPage.xaml

Win8App_Screen 

XML
<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)

C#
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

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

Temporary Storage 

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
MCSD, MCTS, MCP
Personal Blog : http://www.codetoastdev.com/

Comments and Discussions

 
QuestionAbout Windows 8 App - Data storage in files using Windows.Storage namespace Pin
Member 1209249521-May-16 4:50
Member 1209249521-May-16 4:50 
QuestionNot an article... Pin
Dave Kreskowiak10-May-13 10:45
mveDave Kreskowiak10-May-13 10:45 
AnswerRe: Not an article... Pin
Surender Singh (CodeToastDev)11-May-13 2:26
Surender Singh (CodeToastDev)11-May-13 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.