Click here to Skip to main content
Licence CPOL
First Posted 16 Jun 2007
Views 20,303
Downloads 375
Bookmarked 34 times

A flexible way to store application/user settings

By | 16 Jun 2007 | Article
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)

About the Author

Mats Gudmundsson

Web Developer

Sweden Sweden

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFile location Pinmemberjmcilhinney18:15 17 Apr '09  
GeneralFailed to create component 'Settings' PinmemberDean K22:17 20 Aug '07  
GeneralRe: Failed to create component 'Settings' PinmemberMats Gudmundsson8:31 21 Aug '07  
QuestionUse without Settings.dll? Pinmemberjustin.moses7:12 22 Jun '07  
AnswerRe: Use without Settings.dll? PinmemberMats Gudmundsson4:04 23 Jun '07  
QuestionFiles?? Pinmemberpablleaf3:57 18 Jun '07  
AnswerRe: Files?? PinmemberMats Gudmundsson4:58 18 Jun '07  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 17 Jun 2007
Article Copyright 2007 by Mats Gudmundsson
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid