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.

Today at work i was refactoring my code as I do everyday, when it suddenly hit me that our ASP.NET projects were also refactored in a sense. For those of you who don't know what refactoring is, here's a definition.

Refactoring is making changes to a body of code in order to improve its internal structure, without changing its external behavior”.

In layman’s terms it means the process of cleaning your code and break it up in smaller more maintainable pieces. It’s a discipline most programmers are accustomed to and have been using for years no matter the programming language or platform.

The principle of refactoring should also be upheld when designing the structure of your ASP.NET projects. This is typically done by moving code to user controls for reuse and maintainability. However, there is a big difference in refactoring a C# class library and the ASP.NET pages/controls.

Whereas refactoring is an ongoing process in a class library type of situation, it is more a static one in the ASP.NET scenario or much more difficult to do. For instance, if you want to move a region of a page to a user control, you have to move the little individual pieces as well. The list of things to move usually contains the HTML, JavaScript, image references and the C#/VB.NET logic. That’s a whole lot more than you would do in a class library situation and that’s why you have to do a lot of planning before you create your ASP.NET architecture.

That kind of architecture is very important because of the difficult refactoring process in ASP.NET, and that’s why you should plan ahead before you start writing your pages and user controls. It’s a tough job to correct when your project grows bigger and more complex and projects tend to do just that. I know refactoring isn't the same as designing the structure, but it's probably the closest thing we can get to refactoring ASP.NET pages and user controls.