In Visual Studio, you can do Add
-> Existing Item
and then Add as Link
from the Add
drop down button.
This is great. This let's you add a file from another project, and editing the file also edits it in the original project.
I would like to use this feature to have a config file (named Shared.config) be present in all projects within one solution. And have that file always be the same.
solution
|
|- project 1
|
- Shared.config [physical]
|- project 2
|
- Shared.config [linked]
After publishing, the file indeed ends up in all published projects, so no problem there.
But BEFORE publishing (during development on build) the linked file doesn't really exist. Trying to see the file in the Windows Explorer proves that the file is not in the project directory. Visual Studio only makes it look as if it exists there in the solution explorer.
(Though on build, linked items are probably copied to the bin
directory; But I don't want to use/access files from the bin
directory.)
Now this gives problems off course. Trying to print out System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("Shared.config"))
will fail before the project has been published, because of the fact that Shared.config doesn't exist in the project root directory yet.
What I would like to do, and where I need your help is:
- I would like to ON BUILD copy all linked files from their original location to their target location.
This will make visual studio have the linked file, and a copy of the original, to exists both in the same directory with the same name.
Normally , VS won't allow you to create a linked item in a directory if that directory already contains a file with the same name.
But, I have tested by creating a linked item first; then using Windows Explorer to copy the original file to the destination directory, and see Visual Studio act ok. The solution explorer simply hides the physical file, and shows the linked item in stead. (Even if you click Show all files
in the solution explorer.)
solution
|
|- project 1
|
- Shared.config [physical]
|- project 2
|
- Shared.config [linked]
|
- Shared.config [physical, copied here during build, invisible to Solution explorer]
This is exactly what I want! When trying to edit the file, Visual Studio will open the 'linked item'. And on build, a physical file will be copied to the target directory so it exists for the code that tries to access it.
Now how do I do this? Should this be done with Build events? If so how do I say 'copy originals of all linked files to their destination directory?