Click here to Skip to main content
15,861,172 members
Articles / Desktop Programming / Windows Forms
Article

Easily store form- and application-settings using Isolated Storage

Rate me:
Please Sign up or sign in to vote.
4.67/5 (13 votes)
26 Jan 20051 min read 85.6K   504   39   22
A solution to easily store form- and application-settings using Isolated Storage.

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:

XML
<?xml version="1.0" encoding="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:

C#
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:

C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Satellite Provider Satellite Provider
Since 1995 I have gathered wide experience in IT, in several different roles and in different environments. I am currently working for Capgemini Netherlands as Microsoft Software Developer, Software Architect and Project Manager.
In my leisure time I like to do sports, mainly speedskating and windsurfing. I also love to go cycling and to travel with my girlfriend and our son in camper.

Comments and Discussions

 
SuggestionCiertos valores no deben obtenerse al carga - Some values must not be getted on load Pin
Member 132350945-Jul-20 1:42
Member 132350945-Jul-20 1:42 
GeneralVb.Net Version Pin
r3xx0n2-Jun-11 6:15
r3xx0n2-Jun-11 6:15 
GeneralThanks a lot Pin
Malisa Ncube13-Apr-10 6:01
Malisa Ncube13-Apr-10 6:01 
QuestionPlease could you provide me with VB.NET code of this project? Pin
Member 39260143-Sep-09 19:27
Member 39260143-Sep-09 19:27 
GeneralC++ version Pin
dkpeall7-Dec-08 7:56
dkpeall7-Dec-08 7:56 
GeneralRe: C++ version Pin
Edwin Roetman7-Dec-08 8:31
Edwin Roetman7-Dec-08 8:31 
GeneralOn request, VB.NET code is available. Pin
michaeltaylor10-Jul-08 4:31
michaeltaylor10-Jul-08 4:31 
GeneralRe: On request, VB.NET code is available. Pin
Edwin Roetman7-Dec-08 8:32
Edwin Roetman7-Dec-08 8:32 
GeneralRe: On request, VB.NET code is available. Pin
michaeltaylor9-Dec-08 7:44
michaeltaylor9-Dec-08 7:44 
GeneralRe: On request, VB.NET code is available. Pin
Edwin Roetman10-Dec-08 13:15
Edwin Roetman10-Dec-08 13:15 
GeneralSerialize an Array Pin
dgortemaker25-Jun-07 22:43
dgortemaker25-Jun-07 22:43 
GeneralRe: Serialize an Array Pin
Edwin Roetman9-Jul-07 9:42
Edwin Roetman9-Jul-07 9:42 
GeneralLoosing settings Pin
Knuckles9227-Mar-07 6:05
Knuckles9227-Mar-07 6:05 
GeneralRe: Loosing settings Pin
Edwin Roetman27-Mar-07 11:18
Edwin Roetman27-Mar-07 11:18 
QuestionHow do you store data and configuration? Pin
Dewey Vozel4-Feb-05 2:47
Dewey Vozel4-Feb-05 2:47 
AnswerRe: How do you store data and configuration? Pin
gxdata5-Feb-05 16:51
gxdata5-Feb-05 16:51 
GeneralRe: How do you store data and configuration? Pin
Dewey Vozel8-Feb-05 3:44
Dewey Vozel8-Feb-05 3:44 
AnswerRe: How do you store data and configuration? Pin
Erwyn7426-Feb-05 6:42
Erwyn7426-Feb-05 6:42 
AnswerRe: How do you store data and configuration? Pin
Ken Hadden18-Apr-07 10:18
Ken Hadden18-Apr-07 10:18 
GeneralRe: How do you store data and configuration? Pin
Dewey Vozel18-Apr-07 12:54
Dewey Vozel18-Apr-07 12:54 
GeneralRe: How do you store data and configuration? Pin
Ken Hadden22-Apr-07 11:37
Ken Hadden22-Apr-07 11:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.