Using Isolated Storage for your Windows Phone 7 (WP7) App

by Chris Bennett

Has your Windows Phone 7 (WP7) app advanced to the point where you need local storage? Check out Isolated Storage for storing your settings as well as local files.

Similar to other smart phone operating systems, Windows Phone 7 (WP7) does not allow applications to directly access the local file system as you would on Windows. The purpose for this restriction is to prevent other applications from making changes to your application's data. Thus, in order to access your application's data you will need to use the System.IO.IsolatedStorage namespace. Isolated Storage provides support for accessing settings as well as read/writing files.


Just about every application will have the need to store some level of settings for user preferences. To access settings first we need to create the IsolatedStorageSettings object as shown below. (Again these classes exist within the System.IO.IsolatedStorage namespace).

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

It is common to declare a private appSettings object within pages you need to access settings. The appSettings object is basically a dictionary and can be used as such.

appSettings["Setting1"] = "New Value";

string value = (string)appSettings["Setting1"];

As you can see these statements show how to set and retrieve a setting called Setting1. For retrieving a setting you will need to specify the type you need. Any changes made to the appSettings object will automatically save when it goes out of scope or you can explicitly call Save().

Local File Storage

Local File storage through Isolated Storage is the primary method for storing data on WP7. Similar to settings we first need to create an IsolatedStorageFile object as shown below.

using (IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForApplication())

Using the myStorage object we can now perform many of the common tasks you would expect including: creating/deleting folders, checking for existing files, create/open/delete files, etc. Here is a simple example of how to create a data directory and access a single file within it.

using (IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
 //Create the data directory if it doesn't exist 
 if (!myStorage.DirectoryExists("data")) 
 string filepath = "data/test.txt"; 
 using (IsolatedStorageFileStream fstream = myStorage.OpenFile(filepath, System.IO.FileMode.OpenOrCreate)) 
 // Read/Write data 

As you can see from the above code we are creating the myStorage object to get access to Isolated Storage. Then we check to see if our data directory exists and if not, create it. Next we open/create a file called test.txt in the data directory. Once you have the stream for the file you can then perform the various tasks you would expect to perform such as reading/writing data, serialize/deserialize objects, etc.


As you see, using Isolated Storage for storing settings and/or traditional file data is a rather simply process. However, it is important to note that for the initial release of WP7, Isolated Storage is the only method of storing data locally on the phone. In other words, WP7 does not include a local database such SQLite or other similar SQL database. As of the time of this writing, future versions of WP7 including Mango to be released late 2011 will include a local database. In the meantime, Isolated Storage is the primary means of storage. Nonetheless, Isolated Storage is easy to utilize and with a little creativity it is possible to suit your local storage needs.

This article was originally published on Monday Sep 12th 2011
Mobile Site | Full Site