Understanding Isolated Storage

So far in my examples I allowed the user to input some information, but I’ve not done anything useful with that information.

So, today I am going to use the phone’s own storage capacity, its Flash drive to store the information.

In Silverlight applications I don’t get free access to the entire phone’s file system. The user is protected from portentous, malicious applications. And it does that by providing each application with its own personal storage area. That is called Isolated Storage. That makes sense, because each storage area is isolated from its ‘neighbor’ in applications.

So, how do I store to and retrieve data from Isolated Storage?

I am just creating a new project and I am going to add a few buttons and a textbox  into the content panel:

Now I am going to add some code to the event handler that is going to address the Isolated Storage. It is going to create a file and save, whatever the user typed into the textbox, into that file:

First I am creating a new instance of IsolatedStorageFile by creating a new variable called appStorage. GetUserStoreForApplication is going to retrieve a reference to an object that will allow me to interact with this specific application’s Isolated Storage area. “var” is a special keyword, called Local Type Inference. It allows C# to assign the data type when the application is compiled based on what the variable is initialized to. C# will figure out the data type that is returned from this method, GetUserStoreApplication, and create a reference, a variable called appStorage.

GetUserStoreForApplications returns a reference to this application’s Isolated Storage space, so that I could write and read files from it.

With the next line (string fileName = “simple.txt”) I am just hard coding the name of a file. This will be the file name that I am going to store on the phone.

The next line looks a little bit more complex:

using(var file=appStorage.OpenFile(fileName,System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))

I am telling C# the file name and the FileMode, in other words, I am telling C# how I want this file to be treated. If it exists, open it, if not, create it. And finally I am giving permission on what activity I want to perform on that file. In this case I am allowing access that data can be written on that file.

The keyword “using” in that context forces the file that I am working with (inside the open and close parenthesis) to close once the end of the code block is reached. This reduces the possibility that I accidentally leave that file open and accidentally corrupt that file somehow when I try to open it a second time and write more information into it. So, “using” defines a code block, and once the CLR is finished executing that code block, then the file will be closed automatically. Basically, what happens is, that reference (var file) will be set to Null, it will go out of scope and it will be removed from the phone’s memory.

In the next line of code I am creating a reference to a StreamWriter object:

using (var writer = new StreamWriter(file))

This is an object that knows how to write data into an open file.

Ok, let’s move on and add some code to the Clear button:

This just cleans out whatever text is already in that textbox.

Now I want to add some code to the Open button. The idea is, now that I’ve saved some data to the Isolated Storage I want to retrieve that “simple.txt” file from Isolated Storage and then display its content into my infoTextBox:

First I am getting a reference to the application’s Isolated Storage area. Then I am using a StreamReader to get a reference to the “simple.txt” file, and open it up for read only access. And finally I am using the StreamReader’s ReadToEnd method to take care of the details of retrieving the data from the “simple.txt” file, so that I can display it in the infoTextBox.

When I run it now, I can type in some data (1), I can save it (2), clear it (3), and by clicking the Open button (4) retrieve that data again:

Retrieving the data back by clicking the Open button would not work anymore if I would close down the emulator and start a new session. It only works for the current session.

To be continued…

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: