.Net, Windows Phone 7 WP7 – Working With Isolated Storage

1 Comment

In this tutorial, I will demonstrate how to work with Isolated Storage in Windows Phone 7. This is going to be a very simple tutorial where we create a text file and write data to it. We will then read that data and append more data to it.

So first, we need to add these using statements to the top of the code(if they aren’t already there):

using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;

Next we will have our form. Very basic. It has two Textboxes and a Button. The first TextBox will allow the user to type in some text. The Button will save the data to IsolatedStorage, then read the data from the file and put it into the second TextBox.

Here is the XAML:

<Grid x:Name="ContentGrid" Grid.Row="1">

    <TextBox
        Name="txtText"
        Height="128"
        HorizontalAlignment="Left"
        Margin="8,71,0,0"
        VerticalAlignment="Top"
        Width="460"
        TextWrapping="Wrap"
        FontSize="16" />

    <Button
        Name="btnSave"
        Content="Save"
        Click="btnSave_Click"
        Height="115"
        HorizontalAlignment="Left"
        Margin="62,203,0,0"
        VerticalAlignment="Top"
        Width="336" />

    <TextBlock
        Name="textBlock1"
        Text="Text to add to file:"
        Height="30"
        HorizontalAlignment="Left"
        Margin="24,49,0,0"
        VerticalAlignment="Top" />

    <TextBlock
        Name="textBlock2"
        Text="Data currently in file:"
        Height="30"
        HorizontalAlignment="Left"
        Margin="24,342,0,0"
        VerticalAlignment="Top" />

    <TextBox
        Name="txtCurrentText"
        Height="128"
        HorizontalAlignment="Left"
        Margin="6,364,0,0"
        VerticalAlignment="Top"
        Width="460"
        TextWrapping="Wrap"
        FontSize="16" />

</Grid>

So now we move to the code. We need to create an IsolatedStorageFile object:

using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{

}

Since we will always be appending to the file, we need to check to make sure the file exists. If it doesn’t exist, we need to create it.

// we need to check to see if the file exists
if (!isoStorage.FileExists(fileName))
{
    // file doesn't exist...time to create it.
    isoStorage.CreateFile(fileName);
}

Once we have done the FileExists check, we can now open the file. Once the file has been opened, we can now use the StreamWriter class to write to it.

// since we are appending to the file, we must use FileMode.Append
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
{
    // opens the file and writes to it.
    using (var fileStream = new StreamWriter(isoStream))
    {
        fileStream.WriteLine(txtText.Text);
    }
}

Note: notice that we opened the file using FileMode.Append. Because we used this mode, we can’t read from the file, we can only write to it.

So to read from the file, we will need to open the file again, this time using FileMode.Open. We are going to do this in another using block. Once the file is opened, we use the StreamReader class to read it.

// you cannot read from a stream that you opened in FileMode.Append.  Therefore, we need
//   to close the IsolatedStorageFileStream then open it again in a different FileMode.  Since we
//   we are simply reading the file, we use FileMode.Open
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStorage))
{
    // opens the file and reads it.
    using (var fileStream = new StreamReader(isoStream))
    {
        txtCurrentText.Text = fileStream.ReadToEnd();
    }
}

And that’s it. If you run the application, type in the first textbox, hit Save, the data will show up in the bottom textbox. Do it again, and you will see that it has appended to the file.

As you can see, writing to Isolated Storage is really easy.

Here is the entire code for the form…

using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Phone.Controls;

namespace WP7IsoStorageDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string fileName = "MyTextfile.txt";

            using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // we need to check to see if the file exists
                if (!isoStorage.FileExists(fileName))
                {
                    // file doesn't exist...time to create it.
                    isoStorage.CreateFile(fileName);
                }

                // since we are appending to the file, we must use FileMode.Append
                using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
                {
                    // opens the file and writes to it.
                    using (var fileStream = new StreamWriter(isoStream))
                    {
                        fileStream.WriteLine(txtText.Text);
                    }
                }

                // you cannot read from a stream that you opened in FileMode.Append.  Therefore, we need
                //   to close the IsolatedStorageFileStream then open it again in a different FileMode.  Since we
                //   we are simply reading the file, we use FileMode.Open
                using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStorage))
                {
                    // opens the file and reads it.
                    using (var fileStream = new StreamReader(isoStream))
                    {
                        txtCurrentText.Text = fileStream.ReadToEnd();
                    }
                }
            }
        }
    }
}

Tags: , , , ,

One Response to “WP7 – Working With Isolated Storage”

  1. Jason Says:

    When putting this into my .cs file, there are two errors,

    fileStream.WriteLine(txtText.Text); – txtText.Text

    The name textText.Text does not exist in the current context

    Same goes for;
    txtCurrentText.Text = fileStream.ReadToEnd(); – txtCurrentText.Text

    Just thought i would let you know

Leave a Reply