Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #19: Using Isolated Storage

Silverlight uses Isolated Storage as a virtual file system to store data in a hidden folder on your machine. It breaks up the data into two separate sections: Section #1 contains administrative information such as disk quota and section #2 contains the actual data. Each Silverlight application is allocated its own portion of the storage with the current quota set to be 1 MB per application.

Advantages:

  1. Isolated Storage is a great alterative to using cookies (as discussed in Tip of the Day #18) especially if you are working with large sets of data. Examples of use include undo functionality for your app, shopping cart items, window settings and any other setting your application can call up the next time it loads.
  2. Isolated storage stores by user allowing server applications to dedicate unique settings per individual user.

Possible Pitfalls:

  1. Administrators can set disk quota per user and assembly which means there is no guarantee on space available. For this reason, it is important to add exception handling to your code.
  2. Even though Isolated Storage is placed in a hidden folder it is possible, with a bit of effort, to find the folder. Therefore the data stored is not completely secure as users can change or remove files. It should be noted though that you can use the cryptography classes to the encrypt data stored in isolated storage preventing users from changing it.
  3. Machines can be locked down by administrative security policies preventing applications from writing to the IsolatedStorage. More specifically, code must have the IsolatedStorageFilePermission to work with isolated storage.

All that said, let’s take a look at how we save and load data. Note that you will need to add a using statement to reference the namespace System.IO.IsolatedStorage as well as System.IO.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
 
namespace SilverlightApplication10
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            SaveData("Hello There", "MyData.txt");
            string test = LoadData("MyData.txt");
        }
 
        private void SaveData(string data, string fileName)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
                {
                    using (StreamWriter sw = new StreamWriter(isfs))
                    {
                        sw.Write(data);
                        sw.Close();
                    }
                }
            }
        }
 
        private string LoadData(string fileName)
        {
            string data = String.Empty;
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf))
                {
                    using (StreamReader sr = new StreamReader(isfs))
                    {
                        string lineOfData = String.Empty;
                        while ((lineOfData = sr.ReadLine()) != null)
                            data += lineOfData;
                    }
                }
            }
            return data;
        }
    }
}

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Silverlight uses Isolated Storage as a virtual file system to store data in a hidden folder on your machine

# July 16, 2008 2:16 PM

Community Blogs said:

Corrina Barber updated Red & Black skins, Mike Snow on Isolated Storage, Tim Heuer on S3 and SL2B2

# July 17, 2008 1:44 AM

Mirrored Blogs said:

Post: Approved at: Jul-17-2008 Silverlight 2 Update Supposed to have better support for Firefox 3: http

# July 17, 2008 6:25 AM

hangkous said:

When SaveData's data parameter contains CR/LF's they are written to disk. However the LoadData function removes all CR/LF's and returns a single line of text.

# July 17, 2008 10:22 AM

Dew Drop - July 17, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - July 17, 2008 | Alvin Ashcraft's Morning Dew

# July 17, 2008 12:28 PM

Visual Web Developer Team Blog said:

6 new Silverlight tutorials are completed! Tip of the Day #15 - Communicating between JavaScript &

# July 17, 2008 5:19 PM

Visual Web Developer Team Blog said:

6 new Silverlight tutorials are completed! Tip of the Day #15 - Communicating between JavaScript &

# July 17, 2008 5:19 PM

Pietro Brambati Blog said:

Updated 18/7/2008 Percorso formativo in italiano Aggiornamento del percorso formativo su Silverlight

# July 18, 2008 8:21 AM

Pietro Brambati Blog said:

Updated 18/7/2008 Percorso formativo in italiano Aggiornamento del percorso formativo su Silverlight

# July 18, 2008 8:26 AM

D??ng Isolated Storage « Nam Gi?? said:

Pingback from  D??ng Isolated Storage « Nam Gi??

# August 11, 2008 4:56 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# September 22, 2008 6:01 PM