Custom Configuration Sections

AppSettings in app.config is great for making your app configurable without having to recompile it. You specify a key to get a value to avoid hardcoding configurations.

There are two advantages to creating your own custom configuration over basic appSettings:

  1. Enumerate a list of configurations without having to hardcode each key in your code.
  2. Store multiple values per key.

Here’s an example I came up with to illustrate how appSettings could be used. Let’s say we want to monitor one of our servers and make sure something happens if it goes down.



Note that we can alter “value” in config without having to rebuild the app.

However, if we have a collection of servers we’d end up with something like this:



Adding another server requires adding a new key and code change, totally killing the advantage of reconfiguring the app without having to rebuild the code! We need a better approach.

Custom Configuration

Thankfully .NET provides us with a better solution: Custom Configuration sections!


We can define as many custom sections as you wish inside configSections.

  • “name” is the name of our custom configuration section: servers.
  • “type” specifies the class: ServerSection to handle the custom section, and assembly where the class resides: LearnConfigCollection


Now we can loop through them instead of hardcoding the key for each server. Furthermore, each server could have as many attributes as we desire: description, email address of who should be alerted for each server, etc.

How to

The class to handle the custom configuration section consists mostly of boilerplate template. We just need to customize the name and attributes to our needs.

Boilerplate class

The code below might look like a lot, but we’re just sub-classing built-in .NET classes: ConfigurationSection, ConfigurationElementCollection, and ConfigurationElement.

You could easily customize it by replacing the word “Server” with whatever new custom config section that you need.


I hope this tip helps you when you need to keep a list of configurations that does not quite warrant setting up a database table, and yet you do not want to hardcode them either.

Related posts