Basically, App.config and Web.config are easy to deal with when it comes to connection strings and configuration key-value pairs.
But how to access additional sections of those two files?
The only way I can find implies that configuration/configSections/section must be created in order to be able to add additional settings to the configuration files. This is false, since several .NET components actually access custom settings without using configuration/configSections/section. For example, when dealing with diagnostics settings, I can have the section:
<system.diagnostics>
<trace autoflush="true"/>
<sources>
<source name="Samples">
<listeners>
<add name="ConsoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
</listeners>
</source>
</sources>
</system.diagnostics>
without being required to add any configSection to the file.
How to do the same thing in my own code?
The section you mention isn’t undefined, it’s defined in the machine configuration file (which is global for .NET). If you need to have custom sections, you will need to define them in the beginning of the file and define the classes that represent them, so .NET knows how to handle the corresponding objects.
If you only need to access some setting without getting the objects and everything, you can define the section as using just
ConfigSectionHandlerand then accessing the XML document directly like this (after all, the config file is just another XML file).I would strongly suggest to not go the direct access route and instead use the regular way, i.e. this guide. It will save you some trouble by validating everything and making it easily accessible from code. It is a PITA to set up initially, but it’s worth it.