How to Use Isolated Storage in Your WP7 Application

The Windows Phone
platform does not allow traditional file access for data storage. Windows
Phone developers need to be aware of Isolated storage, which provides
managed applications the ability to create and maintain local storage.


based Windows
applications come with the limitations of the Silverlight development
platform, which means no direct local file access. In such scenarios,
application developers have to resort to using what the Silverlight platform
provides for data storage access. Before we dig deeper, we need to understand
that I/O operations in Silverlight are restricted to only isolated storage for
user code (as opposed to platform code, which is written by Microsoft). This
prevents security issues and prevents unauthorized data access.

Isolated Storage APIs

Silverlight for Windows Phone provides the isolated storage
facility via the APIs in the System.IO.IsolatedStorage namespace. Let us look
at the various APIs we can use.


This class represents an isolated storage area. The main
difference between the Windows Phone version and the Silverlight version is
that the Windows Phone platform’s version does not have the API for getting the
user store “GetUserStoreForSite”. Another difference is that the quota for
Windows Phone application is unlimited.


This class provides the ability to store user specific data
as key-value pairs in an IsolatedStorageFile. The SiteSettings property is not
available on the Windows Phone platform.


System.IO.IsolatedStorage.IsolatedFileStream provides a file
stream access to a file stored within isolated storage.


Let us explore creating a Windows Phone application that utilizes
Isolated Storage for writing and reading application data.

Create a new “Silverlight for Windows Phone” application
called “WindowsPhoneIsolatedStorageDemo”.

Create a new Silverlight for Windows Phone application
Figure 1: Create a new “Silverlight for Windows Phone” application

Go to the code-behind of the MainPage.xaml and add the
reference to the System.IO.IsolatedStorage namespace.

// MainPage.xaml.cs


Add a button and a textbox on the page.

Add an event handler for the button’s click event. In this
event handler, we will create (if it does not already exist) a directory called
“MyStorageLocation”. In this function, we will create a file called mytext.txt
and save the contents of the text.

private void buttonSave_Click(object sender, RoutedEventArgs e)
		if (!myISOStore.DirectoryExists("MyStorageLocation"))

			StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream(@"MyStorageLocation\mytext.txt", FileMode.OpenOrCreate, myISOStore));



Now, we create an event handler for the page load event. In
this function, we will check if a directory called “MyStorageLocation” already
exists in the isolated storage. If it exists, we will open the “mytext.txt”
file and fill the textbox with the contents of the file.

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
		if (myISOStore.DirectoryExists("MyStorageLocation"))
			StreamReader readerFile = new StreamReader(new IsolatedStorageFileStream(@"MyStorageLocation\mytext.txt", FileMode.Open, myISOStore));
			textBoxValue.Text = readerFile.ReadLine();


Now, compile and run the application.

The first time we run the application, the text box is
empty. Now type something in the textbox and click Save.

Stop the application. Restart the application. Now when the
application starts for the second time, we find that the textbox contains the
contents we typed during the last run of the application. We see that the
information was stored in an isolated storage location and we have used the
IsolatedStorage APIs to extract the data.


In this article, we saw how to use Isolated Storage for
application data storage. I hope that you have learned how to make your Windows
Phone applications use Isolated Storage.

More by Author

Must Read