devxlogo

Referencing External Configuration Files in .NET

Referencing External Configuration Files in .NET

It is always a good design practice to keep most generalized configuration settings in application configuration files (i.e., app.config or web.config) and very particular settings in external configuration files. Suppose you work with two different sets of connection strings (i.e., one for development and the other for production). You may also need to store other development- or production-specific settings separately. So, every time you want to test your application against one of these sets of databases, you will have to change an entire set of connection strings in your application config file, which involves more error-prone typing.

An alternative approach is to use two separate configuration files (such as dev.config and production.config) representing settings for development and production environments. You can then refer to a required .config file from your application configuration file using the attribute configSource, supported in .NET 2.0 and higher versions.

As an example, to refer to connection strings for use in your development environment, you would write the following in the application configuration file (app.config or web.config), which refers to the dev.config file stored in the config folder:

Similarly, you can use the configSource attribute with other config sections in the application configuration file, such as , , etc.

You can check if this attribute is supported for a given section through Visual Studio IntelliSense.

devx-admin

Share the Post: