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.
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).
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).
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" ?>
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
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.
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.
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.
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,
04-15-2008 7:47 AM