Custom Configuration Settings in app.config

tfowler

Regular
Joined
Aug 16, 2005
Messages
84
Location
Columbus, OH
I spent almost an entire day figuring out how to use custom configuration settings in an app.config.:mad: I never really found any good examples of how it should be done:( , so I decided to share what I learned here.:D

You can always store key/value pairs in the app.config file and read them using the AppSettings function. However, what if you have a more complicated situation where you need to group settings. That is where a custom configuration comes in. Note: you can always create your own XML settings file, but I like having a single place for all of the application settings.

So, say I have 3 different kinds of "things" that have 3 different properties that I want to retrieve.

I set up my app.config file as follows:
Code:
[SIZE=2][COLOR=#0000ff]<?[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]xml[/COLOR][/SIZE][SIZE=2][COLOR=#ff0000]version[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]1.0[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#ff0000]encoding[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]utf-8[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff] ?>[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] <[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]configuration[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]     <!--[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]     define the custom section[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]     note that the 'type' is full name for the class that will be used to access[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]     the custom section and the namespace. We will define this class later[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]     -->[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]     <[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]configSections[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]         <[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]section [/COLOR][/SIZE][SIZE=2][COLOR=#ff0000]name[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]SomeCustomSettings[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#ff0000]                     type[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]ConfigurationExample.SomeCustomSettingsSection, [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]                     ConfigurationExample[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]/>[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]     </[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]configSections[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]     <!--[/COLOR][/SIZE][SIZE=2][COLOR=#008000] here is the start of our custom configuration settings section [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]-->[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]     <[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]SomeCustomSettings[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]> <!--[/COLOR][/SIZE][SIZE=2][COLOR=#008000] this is the ConfigurationSection [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]-->[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]         <[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]things[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]> <!--[/COLOR][/SIZE][SIZE=2][COLOR=#008000] this is the ConfigurationElementCollection [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]-->[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]             <!--[/COLOR][/SIZE][SIZE=2][COLOR=#008000] these are the ConfigurationElements [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]-->[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]             <[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]add [/COLOR][/SIZE][SIZE=2][COLOR=#ff0000]name[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]Thing1[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#ff0000]                    property1[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]Thing1's property1[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#ff0000]                    property2[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]100[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#ff0000]                    property3[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]Thing1's property3[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#0000ff]             />[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]             <[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]add [/COLOR][/SIZE][SIZE=2][COLOR=#ff0000]name[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]Thing2[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#ff0000]                    property1[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]Thing2's property1[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#ff0000]                    property3[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]Thing2's property3[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#0000ff]             />[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]             <[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]add [/COLOR][/SIZE][SIZE=2][COLOR=#ff0000]name[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]AnotherThing[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#ff0000]                    property1[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]Thing3's property1[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#ff0000]                    property2[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]200[/COLOR][/SIZE][SIZE=2]"[/SIZE]
[SIZE=2][COLOR=#0000ff]              />[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]         </[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]things[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]     </[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]SomeCustomSettings[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff] </[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]configuration[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE]

I now need to create classes to access this information.

See the attached files for the complete example (since we are limited a paltry 10000 characters :mad: ).

Hope this example helps someone. I tried to make it much clearer than any of the MSDN documentation or other examples that I found. Let me know if there is anything that I should modify to improve clarity, or if anyone has any questions.

Todd
 

Attachments

Last edited:
Back
Top