Introduction
Isolated storage is a means of saving user-specific data, like saving to the registry or to the good-old .ini-files. To be able to easily use Isolated Storage, I have created a small class that handles writing form-settings (position and size) and application-settings (e.g. user preferences) with a simple interface.
Background
The concept of Isolated Storage is well-described in these articles: Isolated Storage in .NET to store application data and Introduction to Isolated Storage.
The IsolatedStorage.ConfigurationManager class
The IsolatedStorage.ConfigurationManager
class handles reading and writing Isolated Storage. It has the following features:
- All settings are saved in XML files.
- The XML files have the same name as the application but with .config-extension (example: IsoStorageDemoCS.config).
- Form settings are saved by the name of the form.
- Settings saved by form are position, state and size.
- Other settings can be saved by using a unique name per setting.
Here�s an example of a settings file:
="1.0" ="utf-8"
<configuration>
<DemoText>CodeProject Rules!</DemoText>
<DemoChecked>True</DemoChecked>
<DemoCombo>4</DemoCombo>
<Form1WindowState>0</Form1WindowState>
<Form1>235,685,217,162</Form1>
</configuration>
The interface of the class is explained in the following sections.
Save settings using the IsolatedStorage.ConfigurationManager class
To save settings, two calls are sufficient. First create an IsolatedStorage.ConfigurationManager
and then call the appropriate Write
-method. Here's a code sample:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
config.Write("DemoText", txtDemo.Text);
config.Write("DemoChecked", chkDemo.Checked.ToString());
config.Write("DemoCombo", cmbDemo.SelectedIndex.ToString());
config.WriteFormSettings(this);
config.Persist();
}
Retrieve settings using the IsolatedStorage.ConfigurationManager class
Retrieving settings is just as simple as saving:
private void Form1_Load(object sender, System.EventArgs e)
{
IsolatedStorage.ConfigurationManager config =
GetConfigurationManager(Application.ProductName);
config.ReadFormSettings(this);
txtDemo.Text = config.Read("DemoText");
chkDemo.Checked = config.ReadBoolean("DemoChecked", false);
cmbDemo.SelectedIndex = config.ReadInteger("DemoCombo", 0);
}
Round-Up
Using Isolated Storage to store settings can be done very easily. The included demo-project gives a good example. On request, VB.NET code is available.