Loading configuration from an alternative location in Microsoft .NET


I’m continuing my series of posts on configuration patterns in .NET. One of the things I had to do recently was to retrofit some code that I was writing to use an alternate configuration file. That is, I needed the ability to specify file from the commandline to grab a config section rather than having the .NET framework use the default app.config file that resides in the application directory.

It turns out that this is easy to do. There is a method on the ConfigurationManager called OpenMappedMachineConfiguration that will let you define another config file to use.

I have a little method for pulling in the configuration from a given file:

// MSCrmConfigurationSection is my custom config section type
MSCrmConfigurationSection GetConfig( string in_filename ) {
	ConfigurationFileMap fileMap = new ConfigurationFileMap( in_filename );
	Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration( fileMap );
	MSCrmConfigurationSection config = ( MSCrmConfigurationSection )configuration.GetSection( "MSCrm" );
	return config;

A really nice feature that we get for free is that we can mix and match files at will, pulling configuration sections from different files. And, as a bonus, the app.config file still takes effect, so any assembly redirects or publisher policies that are defined there still work as usual. Pretty awesome.


No Responses Yet to “Loading configuration from an alternative location in Microsoft .NET”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: