Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



How to share configuration files between projects

Sharing configuration files between multiple projects in Visual Studio is a simple task, but it is not intuitive in how it is done.  In fact, until recently I did not know how to do it via the IDE, I would always hand edit the .sln file to enable this feature (sad I know).  Today I thought I would throw up a simple set of screen shots to show everyone how easy it really is.

Overview:

We have a solution that has multiple projects and we would like to share our app.config along with our StructureMap.config files between each of the projects.

Step One -- Adding the config files to your soultion:

Create a solution folder in your solution.  I like to add them here as a way of organizing my common files.   Once you have your Solution folder created, right click and choose Add -> New Item (see blow) (If you already have these files, simple do an Add -> Existing item and reference the existing ones).

AddNewSolutionItem

Now for some odd reason MS decided that Application Configuration files were not a valid option for solution folders so we have to do some manual work.  Select the "Text File" template and rename the file to App.Config (see blow).

TextFileTempalte

Finally because we did not choose the Application Configuration template we need to add the XML tags to the file by hand. Here is the XML to add.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>

Step Two - Add reference to the each project:

Now that we have created our config files in our solution folders we want to add them to each project.  We do this by right clicking the project and choosing Add -> Existing Item

AddExistingItem

This will open up a new dialog and this is where the magic happens (the part that always eluded me).  Change the extension type filter to 'All Files(*.*)" allowing us to see the .config files.  Choose the file you want to add, but don't click directly on the "Add" button.  Click the little drop down arrow on the right hand side.  This will give you 2 options.  1) Add 2) Add as Link.  Choose Add As Link.

AddAsLink

After you choose the 'Add as Link' option you will notice that the App.Config file will be added to your project.  But it will has the shortcut image on the icon, this tells you it is a link, not an actual reference.

ShowingAppConfigFile

Step Three -- Setting up the Properties on the file

After we have added our config file we need to ensure that it gets copied to our output (bin) directory during a compile.  *** NOTE *** You do NOT need to do this with an App.config or Web.config as the IDE will do it for you *** END NOTE ***

Right click the linked file and choose Properties.  This will open up a new dialog (fly out pane) and choose 'Copy Always' or 'Copy If Newer' in the Copy to Output Directory action.

CopyAlways

 

There you go, you now have a single config file for your solution that can be referenced by all the projects in your solution.

Hope this helps someone.

Till next time,



Comments

Cornel said:

Why not to add a App.settings file. The .settings file automatically generates the app.config and also you have the designer for adding new values. Am I wrong?

# April 15, 2008 10:28 AM

Chris said:

lol

Someone: "Hey Derik, what did you work on today?"

Derik: "I PISS'd all day...it was awesome!"

# April 15, 2008 10:28 AM

Mike said:

Thanks for sharing this Derik! This has eluded me and I hadn't had time to work it out

# April 15, 2008 10:55 AM

James Gregory said:

Nice tip Derik, I was just cursing having to keep our 3 apps in sync. :)

# April 15, 2008 10:59 AM

Derik Whittaker said:

@Cornel,

How does that help me share the config values across multiple projects.

Also, the IDE designer is no reason to use anything.  If you cannot edit by hand, and understand it you are going to be screwed the first time the designer f$%^'s up your file.

# April 15, 2008 11:26 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# April 15, 2008 11:48 AM

donnou said:

A little off-topic... but I would like to know how to keep different configs among different developers (eg. connection strings) in the same project sharing a common app.config.

# April 15, 2008 10:05 PM

Jason Kealey said:

# April 15, 2008 11:01 PM

Scott Isaacs said:

Nice.  My way around it was to have an app.config "shell" in each project, with each node pointing to a separate file for its settings (connectionstrings.config, appsettings.config, etc.).  The I added a bud event to copy these files.  Your way is much simpler.

# April 16, 2008 12:16 AM

Reflective Perspective - Chris Alcock » The Morning Brew #74 said:

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #74

# April 16, 2008 2:55 AM

Igor said:

Thank you!

# April 16, 2008 6:07 AM

Derik Whittaker said:

@Scott,

Have you noticed any issues with this when using R#.  I actually ran into issues yesterday when i added a external config for my app settings.

# April 16, 2008 6:07 AM

Derik Whittaker said:

In my last post I talked about how you can share a single config file among multiple project in a solution

# April 16, 2008 9:13 AM

Dew Drop - April 16, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - April 16, 2008 | Alvin Ashcraft's Morning Dew

# April 16, 2008 9:56 AM

Chinh Do said:

Derik: Thanks for sharing. This will also work for sharing one AssemblyInfo.cs (for AssemblyVersion and FileVersion) file across projects.

# May 12, 2008 9:39 PM

pratap singh said:

thanks dude,

it really resolves the issue of accessing the config file(linked) of a project in other project added to same solution.

but the becomes useless when the referring project itself contain its app.config file as in this situation CLR reads only one config file that is of referring project not the referred project and simply skips over the linked config files. i even tried to change the name of files like app.config and app1.config but it not at all reads the key of config file in the linked project.

can u help resolve this issue?

# May 16, 2008 7:37 AM

Finds of the Week - May 31, 2008 » Chinh Do said:

Pingback from  Finds of the Week - May 31, 2008 &raquo; Chinh Do

# June 1, 2008 10:51 PM

Ultracet medication. said:

Ultracet.

# June 14, 2008 10:15 PM

Bill Roerts said:

Still work'n in the ghetto...

Using VS2002, I don't have the option to copy the file to the BIN..

The COPY TO OUTPUT DIRECTORY option is not avail to me :(

# August 14, 2008 11:52 AM

DotNetShoutout said:

Your Story is Submitted - Trackback from DotNetShoutout

# November 20, 2008 8:46 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Red-Gate!