Click here to Skip to main content
Click here to Skip to main content

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

By , 10 May 2013
 

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Surender Singh (CodeToast)
Software Developer
United States United States
Member
MCP,MCTS (SQL Server, C#)
 
Surender Singh is a .NET programmer, author and trainer. He is hobbyist programmer and Xbox 360 fan.
 
Blog : http://codetoast.blogspot.com/

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNot an article...mvpDave Kreskowiak10 May '13 - 10:45 
AnswerRe: Not an article...professionalSurender Singh (CodeToast)11 May '13 - 2:26 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 10 May 2013
Article Copyright 2013 by Surender Singh (CodeToast)
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid