The isolated storage is a storage mechanism of Silverlight. Just like the HTML cookies, the isolated storage allows developers to store the data while their application is running, and restore it later when needed. The isolated storage is both user-specific and application-specific, meaning that for a single application, each user will have its own isolated storage, even if two users are running the application on the same computer. Isolated storage is also persistent, which means that even if you clean your browser's cache and temporary files, the data in the isolated storage will stay intact.
The default size of the isolated storage is 1 MB per application. If needed, the size can be increased through a dialog to the user, asking his permission to increase the size. We will discuss how to increase the size of the isolated storage soon.
To demonstrate the use of isolated storage, we will build a simple application that stores and retrieves notes written by the user from the isolated storage. To get started, open the Chapter6-IsolatedStorage solution in Visual Studio 2010.
The project contains a simple UI to save and show notes saved by the user. First, let's add the logic to save a note to the isolated storage. We will begin by getting an instance of the user's isolated storage area by using the GetUserStoreForApplication
method of the IsolatedStorageFile
class. Add the following code snippet to the btnSave_Click
method, which resides in MainPage.xaml.cs:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
Now that we have a representation of the user's isolated storage, we need to create a file that will hold the notes (that is, notes.txt
) and just use the WriteLine
method of the StreamWriter
class to actually save it. Add the following code snippet just below the initialization of the IsolatedStorageFile
class in the btnSave_Click
method:
using (IsolatedStorageFileStream isfs = isf.OpenFile("notes.txt", FileMode.Append)) { using (StreamWriter writer = new StreamWriter(isfs)) { writer.WriteLine(tbNote.Text); } } tbNote.Text = string.Empty;
The preceding code snippet first tries to open a file named notes.txt
from the isolated storage using the IsolatedStorageFile
object's OpenFile
method with a file mode of Append. Append
will first check whether the specified file exists. If it does, Append
will search for the end of that file in order to add new data; if it doesn't, it will create the file. From there we are using the good old StreamWriter
object and just writing the notes the user has written in the textbox to the file.
Now that we have a way of writing the notes, let's add the method that randomly reads a note and displays it. The process of reading data from the isolated storage is quite similar to the writing process. Instead of using the Append
mode for the OpenFile
method, we will use the Open
mode. Instead of using the StreamWriter
object, we will use the StreamReader
object (it does make sense after all; we want to read and not write). Add the following code snippet to the btnRnd_Click
method:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); List<string> notes = new List<string>(); if (isf.FileExists("notes.txt")) { using (IsolatedStorageFileStream isfs = isf.OpenFile("notes.txt", FileMode.Open)) { using (StreamReader reader = new StreamReader(isfs)) { while (!reader.EndOfStream) { notes.Add(reader.ReadLine()); } } } Random rnd = new Random(); int rndNote = rnd.Next(0, notes.Count); noteText.Text = notes[rndNote]; } else MessageBox.Show("no notes for you!");
The preceding code snippet first gets an instance of the user isolated storage and creates a new list of strings to represent the notes. Next, we will perform a check to see if there are any notes saved for this user at all. From there we will use the StreamReader
object to read the file, save its content to a list, and then generate a note with a random number to read and display it for the user.
Build and run the application. Save some notes and try to read them back using the Random button. You should get something similar to the following screenshot:
As you can see, working with the isolated storage is easy and straightforward.
While our little note application may never need more then 1 MB of storage, some other applications you develop might need it. Increasing the storage size is a simple matter of calling the IncreaseQuotaTo
method of the IsolatedStorageFile
object while supplying it with the new size (represented by a long) in bytes. For example to increase the quota to 2 MB, we will pass 2097152
to the method. An example for such a call would be as follows:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); isf.IncreaseQuotaTo(2097152);
As soon as we call the IncreaseQuotaTo
method, a dialog box pops up asking the user to confirm that he wishes to increase the quota size, as shown in the following screenshot:
As soon as the user clicks on Yes, the quota increases.
Another useful property of the IsolatedStorageFile
class is UsedSize
. This property will return the amount of space the application is already using. This property is most commonly used when you wish to calculate the amount of available space you have left in the isolated storage, or when you wish to request more space and need to know how much is already in use.
You can find a sample of implementing the IncreaseQuotaTo
method in the final version of this project within the downloadable content of this book.
18.189.178.53