Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we share some contents of App.config between projects?

Tags:

I have two independent projects in my Visual Studio 2008 solution. Both has its own App.config. But in one project, I need one or two properties defined in another project's App.config. Is it possible to share part of the App.config contents from other project?

like image 712
5YrsLaterDBA Avatar asked Apr 30 '10 18:04

5YrsLaterDBA


People also ask

Where are app config files stored?

Windows uses the %APPDATA% directory for user specific application configuration files.

Does app config get compiled?

Now you might be wondering what happens behind the scenes. Well, when you compile your application, the compiler actually copies the app. config file to the output folder, but gives it another name: When you start your application (ConsoleApp1.exe in our example), the matching config file will be loaded too.

Is app config and web config are same?

When using Windows Forms or WPF etc, then you have the app. config which also stores ConnectionStrings. So yes they are similar but have different purposes.


2 Answers

Yes - of course. Any configuration section can be "externalized" - e.g.:

<appSettings configSource="AppSettings.DEV.config" /> <connectionStrings configSource="MyConnection.config" /> 

or

<system.net>    <mailSettings>       <smtp configSource="smtp.TEST.config" /> 

vs.

<system.net>    <mailSettings>       <smtp configSource="smtp.PROD.config" /> 

Any configuration section can be put into a separate file that can be shared between projects - but no configuration section groups, and unfortunately, it's sometimes a bit tricky to know which is which.

Also, in some cases, Visual Studio will complain (using red wavy underlines) that the "configSource" supposedly isn't valid - but it is - it's defined on the ConfigurationSection object in the .NET config system.

UPDATE:
another feature that hardly enough developers seem to know and use is the ability in Visual Studio to add existing files from a different project as a link:

alt text

With this, you can add links to files into your local project, and they'll always be kept up to date. Great productivity booster if you need to do some file-level sharing (like for common configuration files or such)!

like image 174
marc_s Avatar answered Oct 13 '22 12:10

marc_s


Try this:

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <appSettings file="PROD.config">     <add key="common.Currency" value="GBP" />   </appSettings> </configuration> 
like image 31
Damian Powell Avatar answered Oct 13 '22 12:10

Damian Powell