advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 3.4/5 | Rate this item | 25 users have rated this item.
Expertise: Intermediate
Language: .NET
May 25, 2010
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:

<connectionStrings configSource="config\dev.config" />

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

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

Deepak Choudhari
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement