Home > Articles > Programming > ASP .NET

Isolated Storage

  • Print
  • + Share This
Like this article? We recommend

Persisting Application Settings

Isolated storage answers half of the application settings equation: where to put the files. The other half of the equation is determining what format to use for the file. When application settings were stored in INI files or in the registry, developers typically created a class that would serve as an interface between the storage mechanism and the application itself. The class methods were full of Windows API calls such as RegOpenKeyEx and RegQueryValue, and properties or other methods provided the rest of the application with easier access to the settings.

In .NET programming, there's no need to use special API calls to save the settings. The .NET framework has very broad support for serializing objects in binary and XML formats. Any object can be persisted to any stream by using the built-in serialization support. The result is simpler and more robust code for loading and saving application settings.

I've created a very simple program to illustrate how to create an application settings class and persist the settings between application invocations. The program, called MyEditor, consists of a multiple-line text box and a few menu options for changing the text foreground and background colors. The program persists the color settings across program invocations so that the editor starts with the same colors as were last used. The program uses the EditorOptions class defined in EditorOptions.cs, shown in Listing 2.

Listing 2 EditorOptions.cs

using System;
using System.Drawing;

namespace MyEditor
{
  // marked as Serializable so that I can use
  // binary serializer to save options.
  // Not necessary if using XML serialization.
  [Serializable]
  public class EditorOptions
  {
   private Color backColor;
   private Color foreColor;

   public EditorOptions()
   {
   }

   public Color BackColor
   {
     get { return backColor; }
     set { backColor = value; }
   }

   public Color ForeColor
   {
     get { return foreColor; }
     set { foreColor = value; }
   }
  }
}

EditorOptions is a very simple class that contains public properties for getting and setting the foreground and background properties. The class is marked with the Serializable attribute so that it can be serialized using the binary serializer. If you choose to use the XML serializer, it's not necessary to apply the Serializable attribute to the class.

The main form of the MyEditor program contains roughly 250 lines of C# code, much of it automatically generated by Visual Studio. Rather than include the entire program, Listing 3 shows just the parts that interact with isolated storage. The full program is available for download here.

Listing 3 Listing3.cs

public Form1()
{
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();

  // obtain isolated storage object for this user/domain
  isoFile = IsolatedStorageFile.GetUserStoreForDomain();
}

private void Form1_Load(object sender, System.EventArgs e)
{
  options = new EditorOptions();
  options.BackColor = textBox1.BackColor;
  options.ForeColor = textBox1.ForeColor;
  try
  {
   // Load editor options from isolated storage and apply
   using (IsolatedStorageFileStream isoStream = 
        new IsolatedStorageFileStream(
        OptionsFileName, FileMode.Open, 
        FileAccess.Read, isoFile))
   {
     BinaryFormatter formatter = new BinaryFormatter();
     options = (EditorOptions)formatter.Deserialize(
      isoStream);

     textBox1.ForeColor = options.ForeColor;
     textBox1.BackColor = options.BackColor;
   }
  }
  catch (Exception ex)
  {
   MessageBox.Show(this, ex.Message);
  }
}

private void Form1_Closed(object sender, System.EventArgs e)
{
  // Save editor options to isolated storage
  try
  {
   using (IsolatedStorageFileStream isoStream =
        new IsolatedStorageFileStream(
        OptionsFileName, FileMode.OpenOrCreate, 
        FileAccess.Write, isoFile))
   {
     BinaryFormatter formatter = 
      new BinaryFormatter();
     formatter.Serialize(isoStream, options);
   }
  }
  catch (Exception ex)
  {
   MessageBox.Show(this, ex.Message);
  }
}

In Listing 3, code in the constructor obtains the isolated storage file for the current user, domain, and assembly. This store is used by the code in the Form1_Load() to load application settings at program startup, and by Form1_Closed() to save settings when the program is shut down.

I chose to use binary serialization with the binary formatter for this example. I could just as easily have chosen the SOAP formatter, or opted to use the XML serialization facilities provided by the System.Xml.Serialization.XmlSerializer class. I chose the binary formatter because it provides the most compact storage, and because I didn't foresee a need for users to modify the settings outside of the program.

  • + Share This
  • 🔖 Save To Your Account