Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Visual-Studio / VS2010

Linked Files in Visual Studio 2010

5.00/5 (3 votes)
13 Jun 2011CPOL2 min read 35.3K  
File Linking, File Sharing and Shared Projects

You know how it feels to have shared files on VSS?

Gooood.
Actually it felt so good that my beloved CTO (who said something about promotion?) switched to TFS just 4-5 months ago. And ever since - I cry myself to sleep every night.

Instead of one file, shared between 5, 6, 7 or 10 projects, we now have a copied file for each project. This is due to Microsoft "Best Practice" which says - No Shared Files.

Meaning...?

If I have a JavaScript file called Arrays.js. It's all purpose is to handle arrays in JavaScript. This file exists in 6 different projects, which means that if I have another addition to this small JavaScript library, I have to copy it to 6 different files in 6 different projects...

JUST SHOOT ME!!!

Finally, I have the fix we've all been waiting for, courtesy of VS 2010: "Add File As Link".

How to Link Files to Different Projects

Let’s say we have 2 completely different web sites - one is 10Bet and the other is SportsBettingTech.

I will usually place the original file inside the main project, in this case - 10Bet. I click on the project's name -> Add -> New Item...

A dialog box will open.

I will choose a JavaScript file. Just for testing. We will call it: "LinkedFile.js". I will place a small comment in this file, and Check In.

Now I will go to my second project. Click on the project's name -> Add -> Existing File...

A dialog box will open.

Go to the file you'd like to link to (Hint: It's in a different project's folder), choose it, and instead of clicking the "Add" button, click on that small arrow to the left.

Choose "Add As Link".

And you're done.

How File Linking Works

The point of file linking is that you have one file. This file physically exists in one place (the project it was created in) but has virtual copy of it wherever you want.

If you choose to change this file from the mother project -> after you check in -> you will be able to see the changes wherever you linked it to.

The Million Dollar Question:
"What happens if I change the file from elsewhere?"

That's a great question.

And the answer is: With a very (very) simple solution.

The original copy of the file is being checked out. You will still be your secondary website's solution, but this file will automatically be checked out and displayed on your Visual Studio.

After you change it, simply check in and that's it.

Lots of luck and happiness and good night sleep.

Elad,

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)