Store your settings in isolated storage

Apr 10, 2006

Isolated Storage is a place in Windows where .NET is grated read/write permissions by default. It is a physical folder like any other folder and is located somewhere within C:\Documents and Settings\User name\Local Settings\Application Data\IsolatedStorage. You can read more about it here.

This is a good place to store small files like a setting or configuration file, because you have write permission by default. I have created a simple example of a static setting class called SesttingStore that has two properties which it persists to Isolated Storage. You can add all the properties you want, just remember to modify the Load() and Save() method accordingly.

The first time SettingStore is accessed after the application starts, the static constructor calls the Load method, that fills the properties with values previously saved. When you have changed some of the properties and want to save them to Isolated Storage, just call the Save() method.

#region Using

using System.IO;
using System.IO.IsolatedStorage;

#endregion

/// <summary>
/// Reads and writes settings to the Isolated Storage
/// </summary>
public static class SettingStore
{

#region Constructor

static SettingStore()
{
 Load();
}

#endregion

#region Properties

private static string _RootFolder;

public static string RootFolder
{
 get { return _RootFolder; }
 set { _RootFolder = value; }
}

private static int _Interval;

public static int Interval
{
 get { return _Interval; }
 set { _Interval = value; }
}

#endregion

#region Methods

private const string FILENAME = "settings.ini";

/// <summary>
/// Saves the values to the Isolated Storage.
/// </summary>
public static void Save()
{
 using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
 {
  using (StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(FILENAME, FileMode.Create, isoStore)))
  {
   writer.WriteLine(_RootFolder);
   writer.WriteLine(_Interval);
  }
 }
}

/// <summary>
/// Loads the settings from Isolated Storage if they exist.
/// </summary>
private static void Load()
{
 using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
 {
  if (isoStore.GetFileNames(FILENAME).Length > 0)
  {
   using (StreamReader reader = new StreamReader(new IsolatedStorageFileStream(FILENAME, FileMode.OpenOrCreate, isoStore)))
   {
    _RootFolder = reader.ReadLine();
    _Interval = int.Parse(reader.ReadLine());
   }
  }
 }
}

/// <summary>
/// Deletes the file to clear the settings.
/// </summary>
public static void Clear()
{
 using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
 {
  if (isoStore.GetFileNames(FILENAME).Length > 0)
   isoStore.DeleteFile(FILENAME);
 }
}

#endregion

}

Enjoy.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (3) -

 Jakob Schantz
Jakob Schantz
4/11/2006 6:46:51 PM #

Cool stuff - just what I needed! A secure place to store a secret file.
I use it for my assymetric private encryption key (RSA) which, as you might have guessed, is supposed to be SECRET and only accessible for the server (and me).

Great BLOG you got here, keep up the hacking!

 Mads Kristensen
Mads Kristensen
4/11/2006 8:08:42 PM #

I'm glad you liked the example. Isolated storage is a great place for storing different kinds of files, but it is not a secure place in Windows. From a ASP.NET perspective it is not located in your website root and therefore cannot be accessed from a browser. That's the only security you'll get from Isolated storage. That can however, be enough security you need in your ASP.NET site. It all depends on the type of information you keep in Isolated storage and the level of security you want.

Mark Lichtenstein
Mark Lichtenstein United States
1/23/2008 5:46:33 PM #

Very good article with a complete example.
I have one question on isolated storage, how can you control the time length
for the file? I would like it to persist for a couple of days. It seems to
disappear after an hour. I guess this is a config timeout setting and I may want to
abandon using isolated storage and put the data (screen field values) a database
blob database column along with the user id.

Thanks,

Mark Lichtenstein

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.