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.
private void FormTest_Load(object sender, EventArgs e) {
settings.Open();
textBoxTest.Text = settings.Read("textBoxTest.Text", "No settings file yet!");
}
Add a FormClosing
event in your application.
private void FormTest_FormClosing(object sender, FormClosingEventArgs e) {
settings.Write("textBoxTest.Text", textBoxTest.Text);
settings.Save();
}
How to get started
- Download demo - 7.09 KB - application just to get a feeling of what this is...
- Download source code - 14.9 KB - solution containing the
Settings
class itself and the simple demo. - 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.
- 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!