Over a million developers have joined DZone.

WP7 Saving To Isolated Storage

DZone 's Guide to

WP7 Saving To Isolated Storage

· Mobile Zone ·
Free Resource

IO1In the next few posts, we will be working with Isolated Storage on the phone.  In this post, we are going to cover the basics of saving to, and reading from storage.  As we move forward we will modify this application with more complexity and best practices.

If you have every used the System.IO namespace in your .Net projects, you will find working with the storage on the phone very simple. It is just an extension of that namespace called System.IO.IsolatedStorage.

The first thing we want to do is to create a simple application that allows us to save some data.  We create a PhoneApplicatiton and add some TextBoxes and TextBlocks as shown on the left.

As you can see, we want to capture three simple points of data,  First Name, Last Name, and Age.

If we go to the code behind file, we first need to add the using statements for IsolatedStorage.

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

Next,  lets look at the code that lies under the Set button. As you can see, we first create the storage for the application and then define a streamwriter that we will use to write our data to storage

private void btnSet_Click(object sender, RoutedEventArgs e)
    //get the storage for your app
    IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
    //define a StreamWriter
    StreamWriter writeFile;


Since we will be writing to this file many times, we need to determine if this is the first time that someone has used the application. If it is, we create a folder and a file, if it is not, we just open the file and append it.

if (!store.DirectoryExists("SaveFolder"))
    //Create a directory folder

    //Create a new file and use a StreamWriter to the store a new file in the directory we just created
     writeFile = new StreamWriter(new IsolatedStorageFileStream("SaveFolder\\SavedFile.txt", FileMode.CreateNew, store));
    //Create a new file and use a StreamWriter to the store a new file in the directory we just created
     writeFile = new StreamWriter(new IsolatedStorageFileStream("SaveFolder\\SavedFile.txt", FileMode.Append, store));

Now we gather the data needed, add a comma to seperate the data, and save the data to the file we created in the last step.

StringWriter str = new StringWriter();


            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtAge.Text = string.Empty;


Now that we have the data saved to file. We can retrieve the data using a StreamReader. This is the code under the GET button.

//Get the Store we created earlier
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();

//use a StreamReader to open and read the file           
StreamReader readFile = null;
    readFile = new StreamReader(new IsolatedStorageFileStream("SaveFolder\\SavedFile.txt", FileMode.Open, store));
    string fileText = readFile.ReadToEnd();

    //The control txtRead will display the text entered in the file
    txtReturn.Text = fileText;

    //For now, a simple catch to make sure they created it first
    //we will modify this later
    txtReturn.Text = "Need to create directory and the file first.";

This will read the file and place it in the TextBlock as shown below.


Download the files for this post

In our next post on IsolatedStorage,  we will modify this section to read, search, and delete specific records.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}