Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.
2023-05-10
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.
<configuration>
<configSections>
<section name="featureToggle"
type="System.Configuration.AppSettingsSection"/>
</configSections>
<featureToggle>
<add key="Test" value="Test value is here!"/>
</featureToggle>
</configuration>
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:
ConfigurationManager.RefreshSection("appSettings");
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.
ConfigurationManager.RefreshSection("featureToggle");
_toggles = (NameValueCollection)ConfigurationManager
.GetSection("featureToggle");
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
{
get
{
return _toggles["Test"];
}
}
Good luck with your source code archeology work!
Categories: C#
Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!
Leave a Reply