In 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.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
{
//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.
{
//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.
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.
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.
Download the files for this post
In our next post on IsolatedStorage, we will modify this section to read, search, and delete specific records.
Happy Coding
Daniel Egan – The Sociable Geek