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?
-
1Ditto [link](http://stackoverflow.com/questions/426245/how-to-share-app-config) – nawfal Jun 07 '12 at 13:36
5 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:
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)!

- 21,988
- 13
- 81
- 109

- 732,580
- 175
- 1,330
- 1,459
-
but the target file, such as "MyConnection.config" in your example, must be in the same directory or subdirectory? – 5YrsLaterDBA Apr 30 '10 at 20:41
-
1@5YrsLaterDBA: yes, it has to be in the same directory or a sub-directory thereof. It can be a "linked" file in Visual Studio, however - or a hard link on disk (NTFS link - a "pointer" to another file that looks and acts like "The Real Thing") – marc_s Apr 30 '10 at 20:57
-
Hmmm... I wish I had though of mentioning linking. lol. but you left out something important, marc. – Sky Sanders May 01 '10 at 02:52
-
OK, I added it as a link, now how do I access the various values of the linked app config from the second project? – nawfal Jun 07 '12 at 12:54
-
-
@Justin: just as normal in .NET - just use the [ConfigurationManager class](http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx) ... – marc_s May 08 '13 at 15:56
Try this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="PROD.config">
<add key="common.Currency" value="GBP" />
</appSettings>
</configuration>

- 8,655
- 7
- 48
- 58
-
yeah, but the "file=" attribute is only valid on the
section - nothing else can be put into external files with that method..... – marc_s Apr 30 '10 at 19:27
Only the "running" app.config is used, but you can go external like marc_s says.
You can also create a .Settings file that's "shared". Go to the "shared" project properties, the Settings tab on the left, create a setting with Application scope, and set the Access Modifier on top to Public. In your other project you can then use ClassLibrary1.Properties.Settings.Default.SettingName to access it. It will be strongly typed, but you may need it at compile time.

- 9,436
- 8
- 62
- 81
-
But doesn't the Settings code that is generated rely on the app.config of the running up for persistence? Then you would still need a copy of that file for each running app. – ProfK Nov 02 '14 at 05:30
-
@ProfK: Yes, you're right. The .settings files can have default values which end up as `DefaultSettingValueAttribute` in code, unlike regular app settings in app.config. However, any deviation from the default values would end up in each individual app.config. Since the settings are stored in a section in app.config you could use a combination of the two and externalize it with configSource while keeping the compiled defaults and strong typing. I guess it all depends what you're looking for. – Nelson Rothermel Nov 03 '14 at 22:08
Something I like to do, especially when trying to coordinate ServiceModel elements between libraries and tests is to use configSource to fragment the config in the target library and simply link
/copy always
the fragments in my test projects.
That way I only maintain in one location.
You could take it one step farther and simply have a common directory in the solution and link the fragments in all projects.

- 36,396
- 8
- 69
- 90
In that situation, I would think using a Database to store some configuration data would be ideal. Each app does its own thing, but they look to a shared database to get those common pieces of information.
EDIT: I spoke too soon! Looks like both the OP and I learned something about config files =D

- 40,736
- 10
- 68
- 86