.NET 2.0 Tip: Strongly Typing Configuration Settings

Posted on 10/29/2006 @ 11:17 PM in #Vanilla .NET by | Feedback | 5497 views

Funny – this approach is so darned simple, I am surprised why I hardly find anyone who actually uses it.

Check this out. Let us say, I wish to create an application in which I wish to keep the title of the form and it’s background color configurable, i.e. easily changeable after the application is compiled/deployed. How would I do this?

If you said “Config file” – oh you’d better keep reading.

Yes Config file will work. But there is an easier better way available. The “settings” file. No shit!! Follow these steps:

1.  Create a Winforms app. Right click on the project and add a .settings file as shown below.

2. In the editor that pops up, add the values you wish to store.

You may note that in order to work with the above values, you get the appropriate editor. i.e. no more mistypes.

3. Type in the following code in Form.Load

private void Form1_Load(object sender, EventArgs e)
{
   
this.Text = WindowsAppSettings.Default.ApplicationTitle;
   
this.BackColor = WindowsAppSettings.Default.FormColor ;

}

You would note, full intellisense goodness as you are typing this code.

4. Run the application – seems to work eh?

You would note that the settings file is nothing but a plain XML file as shown below.

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile ..somegooItookout..>
 
<Profiles />
 
<Settings>
   
<Setting Name="ApplicationTitle" Type="System.String" Scope="Application">
     
<Value Profile="(Default)">Some Sample Title</Value>
   
</Setting>
   
<Setting Name="FormColor" Type="System.Drawing.Color" Scope="Application">
     
<Value Profile="(Default)">Red</Value>
   
</Setting>
 
</Settings>

</SettingsFile>

This magic is done by automatically creating a class in the background that strongly typed maps the above values.

Of course, this can be used for more than just global application level configuration settings. It can also support profiles/user level settings.

But I thought this was a cool neat little trick to share. Now I’m hoping you won’t have to create a custom config section, just for the heck of it.

 

Sound off but keep it civil:

Older comments..


On 10/30/2006 12:25:17 AM Matt Hamilton said ..
Sahil,

You don't even need to create a settings file - there's one already in your WinForms project by default, under the "Properties" folder. You access it in code like this:

this.Text = Properties.Settings.Default.ApplicationTitle;

The settings are persisted to your app.config file.

Another cool thing - you can store your own custom classes as settings. Create your class and build your app, then browse to the exe for the type when you're adding the setting. This'll let you use this new type, but it'll add a reference to your exe (presumably in bin\debug) to your project. Remove the reference and everything's good to go!


On 10/30/2006 4:00:43 AM Carl said ..
I think that the new settings stuff in .Net2 is pretty good, I've used it extensively since we migrated from 1.1.

One thing that I wanted to mention was that - and I guess you did what you did to show the basic way of using the settings - is the binding that you can do to the settings file.

Try the following:

Create a new form.


In properties for the form.


Expand (ApplicationSettings)


Click the ellipsis (...) on PropertyBinding


Scroll to 'BackColor' and click the drop down


Choose 'New...' and set up a name and default backcolor for the form.


Click Ok

Now if you change the backcolor in code, maybe use a new button with color dialog:

private void button1_Click( object sender, EventArgs e )


{


ColorDialog cd = new ColorDialog();


cd.ShowDialog();


this.BackColor = cd.Color;


}

Then add the following line to the FormClosing event handler:

Properties.Settings.Default.Save();

Now run up the app and change the color, then close and restart the app.... Voila! No ConfigurationManager in sight!

Carl

PS. This example is using the Default settings file rather than adding another one, of


On 10/30/2006 1:43:21 PM Udi Dahan - The Software Simplist said ..
If you use a container like Spring (http://www.springframework.net), then all you have to do is define properties on the class you want to configure. In Spring's config file, just enter in values there. Voila, no need to even write code that depends on one implementation or another for retrieving configured values.


On 10/30/2006 1:55:49 PM Sahil Malik said ..
Udi -

Is that strongly typed at coding-time?

SM


On 3/14/2007 3:45:37 AM SA said ..
Values created by spring can not be called strongly typed. And spring is for far more better use than just setting form back color or fore color.


On 4/5/2007 12:39:33 AM KNOCKS said ..
The .NET 2.0 configuration stuff is pretty decent.


Anyone remember the sweet Registry days? sigh!


On 4/27/2007 4:33:37 AM Dusty said ..
Hey Carl, 'Property Binding' isn't new to .Net 2.0, it existed in framework 1.1. Just to set straight who deserves credit ;)


On 1/5/2009 2:29:11 AM Raj said ..
Thanks guys, Good stuff.