Isolated Storage in Windows Phone

Introduction

From the current context, we have various Mobile Operating System Options. Since it starts from Java phone. So, since then we have the concept of Isolated Storage or you can say External Storage, in other words SD Card.

Background

In Windows Phone, we have two types of Storage in Isolated Memory.

  • Isolated Storage File
  • Isolated Storage Setting

image.gif

In the case of a file, we have a directory and the files themselves. But, in Settings there is a System Setting that exists for the application all the time.

So, let's start with Isolated Storage Settings.

Since it is just like a Key/Value Pair data type. This means, for every key there must be a value.

image1.gif

So, we will code according to this table.

Hence, a user's preferred language is English and the score is 2000.

Procedure

Step 1: Add a "using System.IO.IsolatedStorage;" for the namespace in your project.

Then, create an instance (object) for the IsolatedStorageSetting class.

image2.gif
Note: In fact, the setting is not an Object.

Step 2: Design a UI that looks as:

image3.gif

Step 3: We will now program the "Save Value" Button (its event).

image4.gif
We are ing the languageTextBox.Text to setting["language"] where language is a User Defined Key and the language Textbox's value is the actual value.

Again, repeat the process for the score key.

Using the Add Method, you can do this, but we have tried a simple one.

Step 4: Next to be done is the Retrieve Button's event.

Until now, we have saved the value of the respective key, now we will try to fetch their value:

image5.gif

In the first If statement, we will check first if there is any value in our settings or not. So, Setting.Contains() will return a BOOL value for that If.

Inside the If, we append setting ["language"] and setting ["score"] with some text.

It's as simple as any string.

Conclusion

First, we need to create a reference for the IsolatedSetting, then assign a value to their setting Key (in other words setting ["KeyName"] =Value).

While retrieving their Value, use the Contains() Method, to check whether they are available in their box or not. If yes, then retrieve using setting ["KeyName"].

Now we will try using IsolatedStorageFile.

Step 1: Before starting, add a "using System.IO;" for that namespace in your project.

Then, create a reference for IsolatedStorageFile, in other words:

image6.gif
After that, create a UI that looks like:

image7.gif

Step 2: So, we begin with a Create Directory Button.

image8.gif
Here, we used the CreateDirectory() method create Directory.

Step 3: Now a Create File Button.

image9.gif
It's the same as the last one, except the CreateFile() method.

Step 4: For the Write Button:

image10.gif
Since we will write something in the Text File, we need a Stream object and for this we will use StreamWriter class.

So, first we created an object for that and in an IsolatedStorageFileStream Object.

In the IsolatedStorageFileStream Object's constructor we four arguments.

They are File location, Mode of Stream, Access Type and FileStream Object.

Then we write in a file using the WriteLine() method then close the stream using the Close() method.

Note: It would be better if you use Try() and Catch() with this. And they probably throw an IsolatedStorageException.

Step 5: For the Read Button:

image11.gif
We will do the same thing as in the Writer Stream, except with the StreamReader class and its methods.

Step 6: For the Clear Button:

image12.gif

Conclusion

Here, we create a Directory and Text file in Isolated Storage (most probably in an External Drive). Then we write (using StreamWriter) some text in the notepad TextBox and retrieve it using Stream Reader.

That's how it works.

The basic difference between them is, IsolatedStorageSetting stores data in memory (RAM) whereas an IsolatedStorageFile stores data in an external drive (SD Card).

So, you are supposed to use a few Setting Keys, since RAM is smaller in size.

Where, External Drives are large in size, so you can use it freely.

Keep designing, Keep developing.


Similar Articles