WP7 and Isolated Storage

Image: renjith krishnan / FreeDigitalPhotos.net
This post contains some good information for using isolated storage on a Windows Phone device.

First of all there are basically three ways an application would like to store data.

  • Temporary data and files
    Your application might need to create temporary data and files while running, then Isolated storage would be a good option. Although just remember to clear the data when it is no longer needed or being used. Create a specific folder that you can regularly clear.
  • User generated data
    Your application might provide the user with the possibility to store notes, photos etc. One thing to take into account here is to provide the user with the possibility to delete this data as well.
  • Application Data
    Settings, selected items in a list etc.

Updating/uninstalling an Application
When an application is updated with a new version, its isolated storage folder will not be touched or modified. So it is up to you to make sure that after an update that the data is still valid and not causing any exceptions/errors. You might need to migrate or transform data to work with the new version.

IsolatedStorageSettings
The easiest way of storing data in isolated storage is to use IsolatedStorageSettings class.

IsolatedStorageSettings.ApplicationSettings["SettingKey"] = "my setting value";

This will get an instance of IsolatedStorageSettings that contains the contents of the application’s IsolatedStorageFile, scoped at the application level, or creates a new instance of IsolatedStorageSettings if one does not exist. When the user exits your application, Windows Phone will automatically call Save to store your application settings.

IsolatedStorageFile
In my application I want to store XML statistics data that is dynamically created during runtime. The Statistics object is a custom class of a Tree data structure. When the application starts I call LoadData and on exit I call SaveData.

To save data to IsolatedStorageFile do as following

        public void SaveData()
        {
            XmlWriterSettings writerSettings =
                new XmlWriterSettings
                {
                    Indent = true,
                    IndentChars = "\t"
                };

            // Save Statistics
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = 
                        store.OpenFile("Statistics.dat", FileMode.Create))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(TreeNode<int>));
                        using (XmlWriter xmlWriter = XmlWriter.Create(stream, writerSettings))
                        {
                            serializer.Serialize(xmlWriter, Statistics);
                        }

                        stream.Close();
                    }
                }
            }
            catch { }
        }

The statistics content will be saved in a file called ”Statistics.dat” in the isolated storage store for the application.

To load data from isolated storage

        public void LoadData()
        {

            XmlSerializer serializer = new XmlSerializer(typeof(TreeNode<int>));
            // Try to load statistics from Isolated Storage
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = store.OpenFile("Statistics.dat", FileMode.Open))
                    {

                        Statistics = (TreeNode<int>)serializer.Deserialize(stream);
                    }
                }
            }
            catch { }
        }

Here you should catch IsolatedStorageException to handle if the file does not exist if you need custom handling for that.
Note, when working with files make sure that you properly use the “Using” keyword to ensure correct use of IDisposable objects.

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: