Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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:

[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

ConfigurationExample.zip

Edited by tfowler

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...