Greycastle Logo

Opening named configuration files in C#

I´ve started using application configurations in C# for almost everything I write.

It´s standard, has good integration and classes all fixed up for you.

However, I´ve found one problem. When testing or otherwise having the need for several different configuration files the ConfigurationManager lacks a method to open any named configurationfile.

You would think:

ConfigurationManager.OpenExeConfiguration("myConfigFile.config");

would work but then you will need an exe that maps to the configuration.

I found this thread that makes use of an ExeConfigurationFileMap to solve this, superb!

var map = new ExeConfigurationFileMap { ExeConfigFilename = "myConfig.conf" };
config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

This way we can open any configuration file regardless of name or exe presence.

© 2024 Greycastle