Click here to Skip to main content
11,934,389 members (31,765 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked

Simple Application Settings Persistence

, 5 Apr 2005
Rate this:
Please Sign up or sign in to vote.
Simple, easily reusable class to persist name/value pair settings for an application


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.


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.


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

//Clears all settings from the configuration if you desire a clean reset.

//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

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


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.


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


About the Author

Dewey Vozel
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralASP.NET version Pin
scaneer7-Apr-05 4:37
memberscaneer7-Apr-05 4:37 
GeneralRe: ASP.NET version Pin
Dewey Vozel7-Apr-05 9:31
memberDewey Vozel7-Apr-05 9:31 
I don't really do too much ASP.NET so perhaps I'm wrong, but the System.Configuration namespace contains an AppSettingsReader class that provides a simple way to read the appSettings section of the app.config file. The app.config file isn't intended to store settings that are changed by the application. It's easy to get around that by reading/writing the configuration file yourself instead of using the System.Configuration namespace but I don't think that's very good practice.

-=( atchr )=-

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 5 Apr 2005
Article Copyright 2005 by Dewey Vozel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid