Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: Add Item / Add as link rather than just Add

Tags:

I'm new to visual studio, coming from Delphi.

I have a directory tree full of .cs files (root is \Common).
I also have a directory tree full of Applications (root is \Applications)
Finally, I've got a tree full of Assemblies (root is \Assemblies)

I'd like to keep my .cs files in the Common tree and all the environment voodoo (solutions, projects, settings, metadata, debug data, bin, etc.) in the Assmblies tree. So, for a simple example, I've got an assembly called PdMagic.Common.Math.dll. The Solution and project is located in \Assemblies\Common\Math. All of its source (.cs) files are in \Common\Math. (matrix.cs, trig.cs, mathtypes.cs, mathfuncs.cs, stats.cs, etc.)

When I use Add Existing Item to add matrix.cs to my project, a copy of it is added to the \Assemblies\Common\Math folder. I just want to reference it. I don't want multiple copies laying around. I've tried Add Existing Item, and used the drop down to "Add link" rather than just "Add", and that seems to do what I want.

Question: What is the "best practice" for this sort of thing? Do most people just put those .cs files all in the same folder as the project? Why isn't "Add link" the default?

Thanks!

like image 662
Pete d'Oronzio Avatar asked Apr 07 '10 15:04

Pete d'Oronzio


People also ask

How do I add a link to a file in Visual Studio?

To add a file as a link, right click and choose Add > Existing Item… as before, but this time, don't click the Add button. Instead, click the little dropdown arrow next to the Add button and select Add as Link. Instead of copying the file into the project directory, Visual Studio will create a link to the original.

How do I add a file reference to a project in Visual Studio?

You can also right-click the project node and select Add > Project Reference. If you see a References node in Solution Explorer, you can use the right-click context menu to choose Add Reference. Or, right-click the project node and select Add > Reference.


2 Answers

You can just use Add As Link by clicking on the little down arrow to the right of the add button from Add-->Existing Item command...

(Thanks Peter)

Whilst I realise this is not in an answer to the original question (which regards best practices), I present this answer in order to save the time of others who have been directed here by the misleading title of this question.

like image 140
BarrieK Avatar answered Sep 20 '22 03:09

BarrieK


The "best practice" in this case, is to not fight the tool. It allows you to do what you want, but you'll get more work done and be able to focus on code if you just let the IDE organize your project for you.

I would create an empty solution project called PdMagic.Common

This will give you a file structure like

PdMagic.Common\ PdMagic.Common\PdMagic.Common.sln 

then I generally add a src and libs folder (via the file system, not VS)

inside the libs folder, i would place all my third party dependencies, and the src folder would hold all of my projects

PdMagic.Common\ PdMagic.Common\PdMagic.Common.sln PdMagic.Common\libs PdMagic.Common\libs\nunit PdMagic.Common\src 

Next, in Visual Studio, I would right click on the Solution I just created, and click "Add -> New Project", I would specify that I wanted it created in the \src folder and call it PdMagic.Common.Math

Now my folder structure would look like this

PdMagic.Common\ PdMagic.Common\PdMagic.Common.sln PdMagic.Common\libs PdMagic.Common\libs\nunit PdMagic.Common\src PdMagic.Common\src\PdMagic.Common.Math PdMagic.Common\src\PdMagic.Common.Math\PdMagic.Common.Math.csproj PdMagic.Common\src\PdMagic.Common.Math\Class1.cs 

Then, as you add classes to your PdMagic.Common.Math project, they will go in the folder with the project file. This is how the IDE has the opinion we should work, and I think most developers go with it because trying to get any other layout on the file system would require too much fighting with the IDE. I know it can be hard to come from a different convention, and you instinctively want the same conventions in the new environment. However, if you stick with the conventions, (right or wrong in your opinion) you'll get more done because you won't be trying to force the IDE to do things the way you think they should be done.

like image 40
NerdFury Avatar answered Sep 20 '22 03:09

NerdFury