jump to navigation

Configuring an application January 14, 2006

Posted by Christian López Espínola in CSharp, Programming, xml.
trackback

At first, sorry for posting so late, I didn’t forgot you. Today we’re going to make easy configuring our applications.
We need a file in our project called “App.config”, which will contain all our application parameters. This is a XML file, with the following format:


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="settingName" value="settingValue"/>
<add key=�anotherSettingName� value=�anotherSettingValue�/>
[more...]
<add key=�Height� value=�200�/>
<add key=�Width� value=�500�/>
<add key=�FormTitle� value=�Hello World�/>
</appSettings>
</configuration>

If you use VS .NET 2005, right click at your project, Add a new item and select Application Configuration File will generate the file “App.config” automatically.

Now in our application we import the System.Configuration namespace, located at the System.dll assembly.
We must use an instance of AppSettingsReader class.


public class MyClass : System.Windows.Forms.Form
{
private AppSettingsReader configReader = new AppSettingsReader();
[...]
}

The AppSettingsReader class implements a method with the following prototype:


object GetValue(string key, Type type)

For example, we will make a Windows Form and configure it like follows (we can make it at the constructor or at InitializeComponent method):


this.Text = (string)configReader.GetValue(“FormTitle�, typeof(string));
this.Height = (int)configReader.GetValue(“Height�,typeof(int));
this.Width = (int)configReader.GetValue(“Width�,typeof(int));

Finally… build and execute! Change values at “App.config” and execute again!

Comments»

1. GeuS - January 14, 2006

good things if are brief two times good. :-)

i didn’t know this way to access to a configuration file.Guess it is the new way using framework 2.0, do not? I used do that using ConfigurationSettings.AppSetings["mykey"] but it is now deprecated.

bye! we are waiting more brief and interesting articles. :-)

2. penyaskito - January 14, 2006

Thanks for reading, Geus.
I saw this way to access to a configuration file used in .NET 1.1 assemblies. This is not a new feature in .NET 2.0.
Like you said, ConfigurationSettings.AppSetings[�mykey�] is now deprecated. The better way to manage our configuration is using ConfigurationManager, of course!

3. pedrolaguna - January 22, 2006

Yuo are designh tu party-cipe in de fibe estrangs avythes. Cee mi bloj four mor infhormation. Zorry x me engli.

4. penyaskito - January 22, 2006

Pedro, I have another blog for these things. You could post it there.

5. Bill Gates - January 25, 2006

Hey, did you know that LINUX r0cks?

Good luck with this blog, and btw, i think it’s a good choice to write it in english. English is superb, I recommend you to read El Quijote in spanglish, here is the very first paragraph:

“In un placete de La Mancha of which nombre no quiero remembrearme, vivía, not so long ago, uno de esos gentlemen who always tienen una lanza in the rack, una buckler antigua, a skinny caballo y un grayhound para el chase”.

Buy the book in amazon.com, i’m sure you’ll find it with DRM as an Extra and with no charge!!!! gee, it’s a golden opportunity!

Sincerely,
the Echelon guy in charge of the ETSII surveillance system.

6. stalker - January 27, 2006

My article about extending web user controls is still pending… you’ll get it once I finished my exams. :)

ASP.NET Live in Penyaskito#… coming soon…