For our approach we want to create one of the *.config files from scratch and populate it with some default/custom values at runtime.
Is there possibility to do this programatically via ConfigurationManager or something like that?
Yes. As you point out, the ConfigurationManager
class allows you to read and write config files.
ConfigurationManager
Class
Scroll down a bit.
Sure, you can read / write these files as XML files, but the above class exposes a much handier interface for manipulating config files.
To trick the ConfigurationManager
into opening an arbitrarily named config file, you can [ab?]useExeConfigurationFileMap
. Note that file
may not exist, in which case it will be created when you call Save()
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap
{
ExeConfigFilename = file
};
var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
//todo manipulate config. add settings / connection strings etc.
config.Save();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With