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

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.

Settings

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")) 
 { 
 myStorage.CreateDirectory("data"); 
 } 
 
 string filepath = "data/test.txt"; 
 using (IsolatedStorageFileStream fstream = myStorage.OpenFile(filepath, System.IO.FileMode.OpenOrCreate)) 
 { 
 
 // Read/Write data 
 
 fstream.Close(); 
 } 
 }

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.

Conclusion

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.



About the Author

Chris Bennett

Chris Bennett is a Manager with Crowe Horwath LLP in the Indianapolis office. He can be reached at chris.bennett@crowehorwath.com.

Related Articles

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

  • Is your sales and operations planning helping or hurting your bottom line? Here are 5 useful tips from the experts at Quintiq to guide you to a better S&OP strategy.

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds