So, I have been trying to write a tool to manage addition/updates to configuration
file like SomeApp.exe.config. This involves reading and writing to configuration files.
Using XML DOM is too low level when 2.0 provides System.Configuration namespace.
Here is the outcome of that effort. Thought I will share in view of the lack of documentation
from MS. Read/Write AppSettings wasnt hard. What I had problems with
was read/write custom ConfigurationSection
Here are a bunch of gotchas from this effort
So having got through that, here is how I did what I did
First off, load the source and target configs
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "source.config";
Configuration source = ConfigurationManager.OpenMappedExeConfiguration(map,ConfigurationUserLevel.None);
map.ExeConfigFilename = "target.config";
Configuration target = ConfigurationManager.OpenMappedExeConfiguration(map,
that we have the source and the target files we can start doing the real work
section in source.Sections)
want to ensure that this guy came from the file we provided.. and not from say machine
is on AppSettings. So we update the appSettings
if (section is AppSettingsSection)
element in source.AppSettings.Settings)
is on a custom section
from target and add from source.
reflection to instantiate since no public ctor and the instance we hold is tied to
ConfigurationSection reflectedSection = Activator.CreateInstance(section.GetType()) as ConfigurationSection;
in framework prevents target.Sections.Add(section.SectionInformation.Name, Section);
Here is what the source looks like. It has one custom section and one appsettings
key value pair
<section name="MyCustomConfiguration" type="Covarius.Configuration.MyCustomConfigurationSection,
Covarius.Configuration, Version=126.96.36.199, Culture=neutral, PublicKeyToken=147be6ea50c5d416"
<MyCustomConfiguration ConfigValue="C:\Test2.ini" MyURL="http://webservices.covarius.dev/testservice.asmx"/>
That was easy wasnt it. I have attached a utility AppConfigUpdater.exe. So you can
use it as a deployment tool to update config in say QA or PROD. Syntax is AppConfigUpdater