Isolated Storage in Windows Desktop Apps using .Net

As we are becoming more and more aware of, giving programs unfettered access to a computer is not a great idea. Unfortunately, many programs still need to save some sort of state data about themselves. To bridge the needs of applications to save data and the desire of administrators and users to use more limited security settings, the .NET Framework supports the concept of isolated storage.

What Is Isolated Storage?

Running code with limited privileges has many benefits given the presence of predators who are foisting viruses and spyware on your users. The .NET Framework has several mechanisms for dealing with running as least-privileged users. By using isolated storage to save your data, you will have access to a safe place to store information without needing to resort to having users grant access to specific files or folders in the file system. The main benefit of using isolated storage is that your application will run regardless of whether it is running under partial, limited, or full-trust.

The IsolatedStorageFile Class

The IsolatedStorageFile class provides the basic functionality to create files and folders in isolated storage.

IsolatedStorageFile Static/Shared Methods

Name Description
GetMachineStoreForApplication Retrieves a machine-level store for calling the Click-Once,application
GetMachineStoreForAssembly Retrieves a machine-level store for the assembly that called
GetMachineStoreForDomain Retrieves a machine-level store for the AppDomain within the current,assembly that called.
GetStore Retrieves stores based on the IsolatedStorage- Scope enumerator
GetUserStoreForApplication Retrieves a user-level store for the Click-Once application that,called
GetUserStoreForAssembly Retrieves a user-level store for the assembly that called
GetUserStoreForDomain Retrieves a user-level store for the AppDomain within the current,assembly that called

How to Create a Store

Before you can save data in isolated storage, you must determine how to scope the data you want in your store. For most applications, you will want to choose one of the following two methods:


This method creates a store to keep information that is specific to the calling assembly and the local machine. This method is useful for creating application-level data.

IsolatedStorageFile machineStorage = IsolatedStorageFile.GetMachineStoreForAssembly();


This method creates a store to keep information that is specific to the calling assembly and the current user. This method is useful for creating user-level data. If you need to specify the user for the store, you will need to use impersonation.

IsolatedStorageFile userStorage = IsolatedStorageFile.GetUserStoreForAssembly();

Example Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.IsolatedStorage;

namespace ConfigurationStorage
    public partial class ConfigurationStorage : Form
        public ConfigurationStorage()

        private void ConfigurationStorage_Load(object sender, EventArgs e)
            comboBox_Car.Items.AddRange(new object[] {
                                                        "BMW","Mercitez Benz", "Honda Civic",
                                                        "Mistibushi Lancer", "Toyota Supra",
            comboBox_Color.Items.AddRange(new object[] { "Red", "Green", "Blue" });
            comboBox_HolDest.Items.AddRange(new object[] { "Singapore", "Paris", "Las Vegas", "Perl Harbour" });
            comboBox_Singer.Items.AddRange(new object[] { "Shakira Mabarak", "Enriquae Iglesias", "Eminem", "Snoop Dogg", "Shina Twain" });
            comboBox_Sports.Items.AddRange(new object[] { "Cricket", "Football", "Tennis", "WWE", "Hockey" });

            comboBox_Car.SelectedIndex = 0;
            comboBox_Color.SelectedIndex = 0;
            comboBox_HolDest.SelectedIndex = 0;
            comboBox_Singer.SelectedIndex = 0;
            comboBox_Sports.SelectedIndex = 0;


        private void button_Save_Click(object sender, EventArgs e)
            IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForAssembly();
            IsolatedStorageFileStream userStream = new IsolatedStorageFileStream("ConfigurationStorage_UserPreferences.settings", FileMode.Create, userStore);

            StreamWriter writer = new StreamWriter(userStream);
            writer.Write("Car:" + comboBox_Car.SelectedIndex.ToString() + "," +
                         "Color:" + comboBox_Color.SelectedIndex.ToString() + "," +
                         "HolidayDestination:" + comboBox_HolDest.SelectedIndex.ToString() + "," +
                         "Singer:" + comboBox_Singer.SelectedIndex.ToString() + "," +
                         "Sports:" + comboBox_Sports.SelectedIndex.ToString() + ",");

        void CheckAndLoadPrefs()
            IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForAssembly();
            string[] files = userStore.GetFileNames("ConfigurationStorage_UserPreferences.settings");

            if(files.Length > 0)
                IsolatedStorageFileStream userStream = new IsolatedStorageFileStream("ConfigurationStorage_UserPreferences.settings", FileMode.Open, userStore);
                StreamReader reader = new StreamReader(userStream);

                string data = reader.ReadToEnd();

                int pos = 0;
                int colonpos = 0;
                int commapos = -1;
                string userObject = "";
                string indexStr = "";
                foreach (char c in data)
                    if (c == ':')
                        colonpos = pos;
                        userObject = data.Substring(commapos + 1, colonpos - commapos - 1);                                               
                    if (c == ',')
                        commapos = pos;
                        indexStr = data.Substring(colonpos + 1, commapos - colonpos -1);
                        int index = Int32.Parse(indexStr);
                        if (userObject == "Car")
                            comboBox_Car.SelectedIndex = index;
                        else if (userObject == "Color")
                            comboBox_Color.SelectedIndex = index;
                        else if (userObject == "HolidayDestination")
                            comboBox_HolDest.SelectedIndex = index;
                        else if (userObject == "Singer")
                            comboBox_Singer.SelectedIndex = index;
                        else if (userObject == "Sports")
                            comboBox_Sports.SelectedIndex = index;

No comments :

Post a Comment

Comment Here