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

, 10 May 2013
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Surender Singh (CodeToastDev)
Software Developer
United States United States
MCSD, MCTS, MCP
Personal Blog : http://www.codetoastdev.com/
Follow on   Google+

Comments and Discussions

 
QuestionNot an article... PinmvpDave Kreskowiak10-May-13 10:45 
Task out the screen shots and the code snippets and there's nothing left.
 
Articles are like research papers. They discuss the topic at hand in depth.

AnswerRe: Not an article... PinprofessionalSurender 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   

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 10 May 2013
Article Copyright 2013 by Surender Singh (CodeToastDev)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid