Click here to Skip to main content
15,892,737 members
Articles / Programming Languages / C#
Article

Simple Application Settings Persistence

Rate me:
Please Sign up or sign in to vote.
3.87/5 (9 votes)
5 Apr 20052 min read 49.4K   890   25   2
Simple, easily reusable class to persist name/value pair settings for an application

Introduction

This class is very simple. It provides an easy way to persist key/value pairs between application occurances. It stores the pairs in an XmlDocument in IsolatedStorage. It is commented for IntelliSense and should be easy to use without much explanation.

Downloads

VisualStudio Solution

The source code is available in the attached solution, containing three projects. The first project is of course the class library that contains one class, ApplicationSettings, that resides in the VastAbyss.Configuration namespace.

One project is a simple Console application that I wrote to include in the source of the main project as an XML comment example. When it executes, it creates an instance of ApplicationSettings. On first run, a new file will be created and the default string will be displayed in the console. The application then takes input from the user and exits. The next time the application runs, it will display the string that was entered the last time; demonstrating that the setting was persisted.

The last project is a Windows application that shows some more use of the class to persist WindowState, Height, Width, and the Text value of some Labels. The on-going theme here is simple persistence, not of user data, but of application settings.

Compiled Class Library Assembly

This download contains only the compiled dll that is needed to be referenced in using the class in a project. I removed all but the source, project, and solution files from the solution before compressing it to keep the size small for your convenience so the I'm including this so you don't have to compile it yourself. The class should be usable and self explanatory without downloading the source code or examples.

Usage

C#
//Creates a new instance of ApplicationSettings
ApplicationSettings appSettings = new ApplicationSettings();
 

//Clears all settings from the configuration if you desire a clean reset.
appSettings.ClearSettings();
 

//Sets an existing value or creates a new key/value pair
bool success = appSettings.SetValue("ApplicationTitle", "My Application");
 

//Retrieves all settings in a HashTable
HashTable hTable = appSettings.GetSettings();
 

//Retrieves the value of a single setting by name.
string appTitle = appSettings.GetValue("ApplicationTitle");
 

//Retrieves the current underlying XmlDocument
XmlDocument xmlDoc = appSettings.XmlDocument;
 

//Removes a key/value pair from the configuration
success = appSettings.RemoveValue("ApplicationTitle");
 

//Manually saves configuration
appSettings.SaveConfiguration();
 

//When disposing, the configuration is saved automatically making the
//above line unnecessary if the object is disposed properly.

appSettings.Dispose();

Future Plans

I do not have plans to extend the class at this time, but I do have some ideas on how it could be easily extended to be more powerful. I would be worried about destroying the simplicity of the class as it is now, but one of the things that would make it more powerful without being overwhelming would be to add a constructor that takes an enum. For example StorageType.IsolatedStorage, StorageType.ApplicationData, StorageType.CurrentDirectory, StorageType.Registry, etc... and a constructor that simply takes a file name.

My feeling on this class is that it needs to remain simple. It is designed to fill a niche: an easy way to persist application state for applications that do not require complex settings or user data storage. As seen in the Windows test project, it can fill that niche without many lines of code.

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralASP.NET version Pin
scaneer7-Apr-05 3:37
scaneer7-Apr-05 3:37 
GeneralRe: ASP.NET version Pin
Dewey Vozel7-Apr-05 8:31
Dewey Vozel7-Apr-05 8:31 

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.