Monday, July 12, 2010

Reusing file in Visual Studio without copying

This is so silly but I just figured out how to do it and I had to post it (in case someone else is as brain dead as I was).
When you want to add an existing file to your current project in Visual Studio that's located outside the current project's directory, that file is copied into the project folder, then it's added to the project.

Well what if you don't want a duplicate file?

Maybe you want to share the same source file in multiple projects so that if you modify the code in one project (to fix a bug, for example) the updated code is immediately available to the other projects.

I know, I know, most of us would create some sort of shared library for this code and just reference this DLL or even the whole VS project, but to add a shared file all you need to do is instead of clicking the 'Open' button when adding an existing file, click the arrow to the left of this button. Then select 'Link File' from drop down, this way you create a link to the original file without coping it locally.
Simple!