Over a million developers have joined DZone.

WP7 Saving To Isolated Storage

· Mobile Zone

Learn how to Deliver Better Mobile Apps Faster with Continuous Quality by managing the complexities of testing multiple devices and scenarios with this whitepaper from Perfecto Mobile.

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
    store.CreateDirectory("SaveFolder");

    //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));
}
else
{
    //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();
            str.Write(txtFirstName.Text);
            str.Write(",");
            str.Write(txtLastName.Text);
            str.Write(",");
            str.Write(txtAge.Text);

            writeFile.WriteLine(str.ToString());
            writeFile.Close();

            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;
try
{
    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;
    readFile.Close();
}

catch
{        
    //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.

IO2 

Download the files for this post

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

Do you know Why Apps Succeed? Perfecto Mobile analyzed over 1,000 responses to their Digial Quality Strategies survey and aim to answer the question, "Why do apps succeed?" in this exclusive report.

Topics:

Published at DZone with permission of Daniel Egan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}