Tuesday, May 29, 2007

User-based Config Files

Here's another useful tip that was prompted after reading this post regarding location-specific config files. In a multi-developer environment it is sometimes necessary to have developer-specific parameters in your config files while maintaining the base configuration file, specifically when using source control. For example, developers may need different connection strings to access different databases during development and testing. In our case the base config file (app.config or web.config) contain a single connection string that is used by the DAL. Therefore, two different developers cannot have separate connection strings in the base config file.

To solve this problem we used the "file" attribute of the section of the config file to specify a user-specific set of configuration parameters. An example of the base config file:


The user.config file contains:

The contents of the section will be merged with the section of the base config file with the settings from user.config superceding those in the app/web.config. Any additional settings in the base config file are preserved, allowing us to have static config parameters in the base config file and only user-defineable parameters in the user config files. Finally, we set the user.config files to be excluded from source control.

4 comments:

Diablo said...

Hey this is cool. Is it possible to keep the user based config file in a virtual directory beneath the application?

I ask because it would be cool to be able to vary the configuration from one environment to the next without modifying your code base.

Let me know what you think.

WhiteHo said...

Diablo,

You should be able to do this. Just specify the relative path to the subordinate config file (eg, "myconfig/user.config").

Diablo said...

I could use a physical folder beneath the directory hosting web.config. Couldn't use a virtual directory pointed elsewhere. Rats.

Aki said...

What if I want to specify multiple files?

~ Aki