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

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Do you know where your data is? Consumer cloud-based file sharing services store your sensitive company data on servers outside of your control, outside of your policy and regulatory guidelines – maybe even outside your country – and not managed by you. The potential for data leakage, security breaches, and harm to your business is enormous. Download this white paper to learn about file sync and share alternatives that allow you to manage and protect your sensitive data while integrating and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds