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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds