Reputation: 1969
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?
Upvotes: 5
Views: 5635
Reputation: 8913
Yes. As you point out, the ConfigurationManager
class allows you to read and write config files.
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();
Upvotes: 11
Reputation: 8966
Since .config files are just XML, you should be able to just use an XmlTextWriter
to build your config file.
Upvotes: 3
Reputation: 75073
sure it is, it's a simple and plain xml file
now... what config
file are you writing about? app.config
? web.config
? custom.config
?
first two are configuration support for the windows app and web app, so you would need to generate from an "outside" app and then fire that app up (witch that already happens with the Setup project where it asks for the user things and you can edit/add to the configuration file, like Database connections, etc)
Upvotes: 0