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.

Introduction

Silverlight based Windows Phone 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.

System.IO.IsolatedStorage.IsolatedStorageFile

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.

System.IO.IsolatedStorage.IsolatedStorageSettings

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

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

Hands-on

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

usingSystem.IO.IsolatedStorage;

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"))
		{
			myISOStore.CreateDirectory("MyStorageLocation");

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

      }

	}

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();
			readerFile.Close();




		}
	}

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.

Summary

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.



About the Author

Vipul Vipul Patel

Vipul Patel is a Software Engineer currently working at Microsoft Corporation, working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com

Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds