Click here to Skip to main content
11,429,399 members (64,563 online)
Click here to Skip to main content

A flexible way to store application/user settings

, 16 Jun 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple class that stores settings in an XML file located anywhere. This class can be used directly as a visual component in VS. Use this as it is or as an inspiration for something better!

Introduction

There are many ways to store settings in an application, but they all seem to have serious drawbacks. The built-in Settings from the Properties window is very nice but we can't choose where to put the file (or what to call it). I like to have full control over both application and user settings. It feels good as a user to know that all files belonging to the application is placed in the application's folder and not spread out in the Registry or in some hidden user folder. This simple class/component gives you the possibility to store simple types as int, double, bool, string, and with some creativity, more complex types, in an XML file. If you liked the old ini files, you will like this too! This is all done in Visual Studio 2005 and the 2.0 framework.

Background

Most things in Microsoft's world are very nice and easy to use. I can't say settings are one of them. This was the best and most flexible way to store settings that I could come up with after spending some days trying different ways. I also would like to keep it pure .NET and do not want to wrap some old Win32 DLL...

Using the code

Add a Load event in your application. You can use the ConfigFile property (in the code or in the designer) to specify the storage file, or just leave it to the default value.

// Application start
private void FormTest_Load(object sender, EventArgs e) {
  // Read settings from file
  settings.Open();
  textBoxTest.Text = settings.Read("textBoxTest.Text", "No settings file yet!");
}

Add a FormClosing event in your application.

// Application close
private void FormTest_FormClosing(object sender, FormClosingEventArgs e) {
  // Save settings to file
  settings.Write("textBoxTest.Text", textBoxTest.Text);
  settings.Save();
}

How to get started

  1. Download demo - 7.09 KB - application just to get a feeling of what this is...
  2. Download source code - 14.9 KB - solution containing the Settings class itself and the simple demo.
  3. Decide how to use Settings in your application. Maybe you want to skip the included design time support to make it even simpler...
    • Just copy and paste from the Settings.cs file into your own source code, or add the Settings.cs file to your project...
    • Add the Settings.dll as a reference to your project...
    • Use it as the demo application, simply keep your project in the same solution, and drag the Settings component from the ToolBox to your form...
    • Add it to the ToolBox to make it available to any solution/project. Put Settings.dll and Settings.xml in some permanent folder like C:\Program Files\Common Files\VisualStudioComponents\... Then right click in the Toolbox and Choose Items... -> Browse... -> ...\Settings.dll.
  4. Expand Settings.cs with your own features. Add support for your own special classes and built-in types.

Points of interest

This small article also gives a first glance into how to create a simple component/control to use at design time in Visual Studio. Inheriting from Component gives you a component that ends up in the component tray when dragging it into some form (just like a timer). Inheriting from Control gives you a component that will stay in your form (like a Button). There is a very good book called "Pro .NET 2.0 Windows Forms and Custom Controls in C#", ISBN 1-59059-439-8. Read it!

If you create user components they might need to store settings. A nice way is to provide a property for the Settings class. This way all the components can store their settings in the same file!

History

  • 2007-06-17: Submitted this article and code.

Please don't forget to vote on this article. I really like to know and learn!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mats Gudmundsson
Web Developer
Sweden Sweden
I'm currently working at DeLaval International AB located in Tumba, Sweden. I work with a robot system called VMS that automatically milk cows. I mostly work on developing software to our built in Linux platform. Today .NET is partly used in our Management software that runs on a XP PC. We might dare to start develop some .NET applications using mono on Linux soon...

Comments and Discussions

 
GeneralFile location Pin
jmcilhinney17-Apr-09 19:15
memberjmcilhinney17-Apr-09 19:15 
GeneralFailed to create component 'Settings' Pin
Dean K20-Aug-07 23:17
memberDean K20-Aug-07 23:17 
Whenever I add Settings.dll as a component I get the message 'Failed to create component 'Settings'...

Any suggestions? Does the project need to have the Namespace EasySettings?
GeneralRe: Failed to create component 'Settings' Pin
Mats Gudmundsson21-Aug-07 9:31
memberMats Gudmundsson21-Aug-07 9:31 
QuestionUse without Settings.dll? Pin
justin.moses22-Jun-07 8:12
memberjustin.moses22-Jun-07 8:12 
AnswerRe: Use without Settings.dll? Pin
Mats Gudmundsson23-Jun-07 5:04
memberMats Gudmundsson23-Jun-07 5:04 
QuestionFiles?? Pin
pablleaf18-Jun-07 4:57
memberpablleaf18-Jun-07 4:57 
AnswerRe: Files?? Pin
Mats Gudmundsson18-Jun-07 5:58
memberMats Gudmundsson18-Jun-07 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.150428.2 | Last Updated 17 Jun 2007
Article Copyright 2007 by Mats Gudmundsson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid