"Add existing item" in Visual Studio: is it possible to make "Add as link" default?
Asked Answered
G

6

15

As pointed out in this SO answer, the Add > Existing item dialog in Visual Studio by default displays the Add button (meaning that the selected items will be physically copied to the new location), whereas the desirable action is often (always?) to Add as Link.

Is it possible to configure Visual Studio so that Add as link is selected by default when opening the Add > Existing item dialog?

I have thoroughly searched the Options dialog in Visual Studio and examined the MSDN documentation for an answer to this, but so far to no avail.

Project Linker would be a good option for automated linking when two new projects are to share the same code base. However, when linking a large number of files from an existing to a new project, one seems to be confined to the Add > Existing item approach, and this work could be much more convenient and less prone to error if Add as link would be the default action.

Grisette answered 1/8, 2012 at 20:43 Comment(5)
If you find yourself using Add as link very often, you might review if there are better approaches out there to organize your source files. For me, that's a red flag I will pay attention to.Postwar
@LexLi The above is a typical scenario when porting an existing .NET Framework class library to a Silverlight or Portable Class Library. I want to keep the original class library as-is, but re-use the classes as much as possible in my ported library. As a comparison, MonoDevelop recognizes when I am trying to add files that are located outside my project folder, and prompts whether I want to physically copy the files or create links. Something similar in VS would be great.Grisette
I did port my library to Mono for Android, but I rather put the csproj files at the same level github.com/lextm/sharpsnmplib/tree/master/SharpSnmpLib, and then I can add files to them directly, without using Add as Link. MonoDevelop follows SharpDevelop's way, which is what you prefer. But Visual Studio has been acting like this for a very long time, and I don't think Microsoft has a plan to change that. For me, Add as Link is "evil", as you could not know where the file resides obviously.Postwar
@LexLi I disagree that it's a red flag. I just found this question when cursing about yet again making the mistake--it's one solution containing two projects with about 95% of the code in common. (Web and desktop front ends to the same back end code.) This is stuff under development, not a library.Nepean
I was able to Add Link for a file from project to another, but was not able to add link in another folder within the same project.Hebdomad
R
18

Unfortunately, there is no option for that. But there is silver lining. You can drag files to a project and drop them to desired folder. Files are copied to your project. If you press Alt key doing drag and drop operation, files are not copied but linked instead.

Ronald answered 12/10, 2012 at 12:13 Comment(5)
Not a problem @edokan, just wanted to make sure your answer was as you actually intended :-) I have also edited it to take advantage of the <kbd> command available in SOJaquenette
Very interesting, but does this really work on VS 2010? I have tried with ALT, ALT GR, CTRL and SHIFT, but in neither the case file links were created?Grisette
In fact I tried this in VS 2012. In VS 2010, Alt key does not work. Sorry about that.Ronald
@edokan No worries, after all I did not specify VS version in my question. I just tried it on VS2012 and it works perfectly! Many thanks for pointing this out, I have not seen it promoted anywhere else. But it makes sense to have it in VS2012 though, now that PCL is a first-class citizen in the IDE. Yet another reason to quickly switch all work to VS2012 :-)Grisette
I just tried this and while it does link the files, it creates clones of the directory structure instead of making links to the folders also. This creates weird situations where if I Add a new Class file from the VS UI, it creates the file in the cloned folder structure under the project instead of creating it in the linked folder. Is there a way to avoid this?Squeaky
T
3

You can also use VSCommands extension which adds 'Copy as Link' on folders and files in Solution Explorer. After that you can do 'Paste Link' in desired location. It also allows you to navigate to source item from linked item via 'Locate Source File' from cotnext menu. Works with vs2010 and vs2012.

Tepid answered 25/11, 2012 at 13:26 Comment(1)
Interesting. Thanks for the tip, Jarek!Grisette
S
0

It appears that when you "Add Existing" to a SOLUTION FOLDER(as opposed to an actual folder), then you are always adding these items as a link. If you add existing items to an actual folder, then you get the button that gives the option for "Add as Link".

Symbolic answered 13/2, 2013 at 22:12 Comment(0)
B
0

Simply Edit project file and change the path of the file. You can relatively move folder up using ../ It starts in the project folder.

Bonney answered 15/2, 2017 at 3:5 Comment(0)
T
0

Just add to your project file the following section

<ItemGroup>
    <None Include="..\README.md">
    </None>
</ItemGroup>

I'm refering the README.md file contains in the upper directory of my current project. The file will appear with a little shortcut icon. You can now edit the file from your project.

Turnip answered 14/2 at 17:20 Comment(0)
M
-3

I found an nice tip here: If you want to add an existing item as link in VS C# Express 2010 in the "Add existing item" dialog you have to explicitly choose Add As Link from the dropdown on the Add button.

Malvaceous answered 22/11, 2013 at 13:0 Comment(1)
Thanks for taking the time to post, but what you are referring to is already described in my question. I would prefer that you remove this answer as it does not add any new information to the issue.Grisette

© 2022 - 2024 — McMap. All rights reserved.