Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Change configuration values in runtime in .NET Framework


I stumpled upon the problem of having to change the configuration values in an old .NET Framework 4.0 system while it is running, and it has been possible to do since .NET Framework 2.0.

The compiled App.config file will be named like the assembly it belongs to with the ending .config added to it (e.g. MyProgram.exe.config). Even if your settings are read from and/or used in a DLL, the settings should be written in the main program’s configuration file, not the DLL’s own config file.

You might know that a running system will load its settings when they are referenced by the ConfigurationManager and keep the settings in memory for the rest of the session, meaning that if you edit the config-file, it will probably have no effect.

If you want to use dynamic loading of the config file, you should consider declaring a custom section in the config file. You can of course put all your settings in the appSettings section, but then reloading your settings will be an all or nothing deal. The upside of using appSettings is that it is pre-defined (just like connectionStrings), meaning that you can access them directly from the ConfigurationManager. For your own sections, you need to declare your own variables to store the settings.

This sample shows how to first register a section named featureToggle and then fill it with only one setting called Test. Remember that this is done in the main executable’s App.config file, no matter where the setting is read or used.


    <section name="featureToggle"

    <add key="Test" value="Test value is here!"/>


A reference to System.Configuration is needed for the code to work.

Like mentioned, if you were using the appSettings section, you could just run this line of code, and your settings will be reloaded:


But in this case, I am using a custom section called featureToggle, so I have to both refresh the section and save the section in my variable.


_toggles = (NameValueCollection)ConfigurationManager

The NameValueCollection is found in the System.Collections.Specialized namespace.

If the _toggles (of type NameValueCollection) variable is initialized in a constructor of a custom configuration class, the constructor will contain the second of the two lines above. And both lines will be needed if you have some sort of reload method in the custom configuration class.

The actual values in the config files are accessed via the _toggles variable, like so:

public string Test
        return _toggles["Test"];

Good luck with your source code archeology work!

Categories: C#

Leave a Reply

Your email address will not be published. Required fields are marked *

En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!


Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   |   |   |   Filmtips