Linking Files in Visual Studio

Posted Wednesday, January 20, 2010 9:29 AM by CoreyRoth

I saw this feature in Visual Studio again the other day and honestly I had kind of forgotten about it.  After discussing it with some colleagues, most of them didn’t even know the feature existed.  What I am talking about is the ability to create a link between files in Visual Studio.  What this allows you to do is actually create a link to a file instead of just making a copy.  Now, I don’t want to get into a discussion of why you would never want to do this or how this is not a proper way to implement code reuse.  Keep in mind though that you can use it to link other types of non-code files such as an XML file.  However, for today’s purpose, I’m just going to link a class into another project.  Consider my following example with two class libraries. I want to reuse Class1.cs inside ClassLibrary2.

FileLinkingSolution

I have a simple class that looks like this.

using System;

 

namespace ClassLibrary1

{

    public class Class1

    {

        public Class1()

        {

        }

 

        public void DoSomething()

        {

            int x = 5;

        }

    }

}

This class clearly has so much valuable code in it that I have to reuse it in ClassLibrary2, but not make a reference to ClassLibrary2.  This is where the Add as Link functionality comes in.  The process is simple, using the Add Existing Item menu on the project’s context menu.  Navigate to the existing item in the other class library and then make note of the arrow next to the Add button, click on it and choose Add as Link instead.

FileLinkingAddExistingItem

Once you do that you will have a link to the file in your other class library.  You can open it and edit it as normal from the linked location, but it will actually edit the file link.  You can tell it is linked in Solution Explorer by the icon.

FileLinkingSolutionLinkIcon

Notice the link icon on Class1.cs in ClassLibrary2.  If you click on the linked file, you can see the path to the file it has linked in the properties (although its cut off here in my screenshot).

FileLinkingProperties

Once you are done here, you can make changes to the file from either class library and the original file gets updated.  Keep in mind though, if you delete the original file, the link will be broken and you will get an error when trying to view any links.  I don’t know how useful this feature will be to all of you, but I think there is a time and place for everything.  I can definitely see this being useful in some cases.  This isn’t a new feature either.  I think its been around since at least Visual Studio 2003, but I could be wrong.

Comments

# re: Linking Files in Visual Studio

Thursday, June 3, 2010 1:03 PM by Wayne Odom

Dug for 20 minutes before I figured this out.  Was trying to figure out what the little arrow was about on the files.   Good blog entry.

# re: Linking Files in Visual Studio

Friday, September 24, 2010 12:10 AM by Josh Painter

Just did a google search for this and you came up first. Small world. Thanks! :)

# re: Linking Files in Visual Studio

Friday, September 24, 2010 8:40 AM by CoreyRoth

@Josh Indeed.  I hope it helped!

# re: Linking Files in Visual Studio

Thursday, October 14, 2010 10:55 AM by Dave

I use it for resource files common between a server project and a client project. Very handy!

# re: Linking Files in Visual Studio

Thursday, January 20, 2011 9:11 AM by marandescu

As Josh, I ended up on your blog following a google search for this feature. straight to the point, thanks :)

# re: Linking Files in Visual Studio

Tuesday, February 15, 2011 10:22 AM by MarceQ

I didn't have the Add As Link Option, I just have the standard Add and the "Show previous versions" options...   anybody knows why?

I'm using VS 2010 Professional....

# re: Linking Files in Visual Studio

Wednesday, March 9, 2011 2:05 AM by Thomas

Thanks for that very userful peace of information!

# re: Linking Files in Visual Studio

Tuesday, July 5, 2011 11:56 PM by Rahul Techie

Not able to see this option for Web Site

# Castle include WCF hosting - Programmers Goodies

Monday, October 10, 2011 10:25 PM by Castle include WCF hosting - Programmers Goodies

Pingback from  Castle include WCF hosting - Programmers Goodies

# The Red Circuit - TFS Build With Octopus Deploy (Part 2)

Monday, January 23, 2012 5:26 PM by The Red Circuit - TFS Build With Octopus Deploy (Part 2)

Pingback from  The Red Circuit - TFS Build With Octopus Deploy (Part 2)

# One App for 7.1 and 7.0 | PHP Developer Resource

Wednesday, May 23, 2012 12:26 PM by One App for 7.1 and 7.0 | PHP Developer Resource

Pingback from  One App for 7.1 and 7.0 | PHP Developer Resource

# re: Linking Files in Visual Studio

Friday, June 29, 2012 3:05 AM by Mable John

Thank you

# Shared Projects für Plugins and Workflows – xRM & .Net Development

Pingback from  Shared Projects für Plugins and Workflows – xRM & .Net Development

# Creating Linked Files in Visual Studio | Zaruba Answers

Saturday, December 13, 2014 1:27 AM by Creating Linked Files in Visual Studio | Zaruba Answers

Pingback from  Creating Linked Files in Visual Studio | Zaruba Answers

Leave a Comment

(required)
(required)
(optional)
(required)