133

In Visual Studio, we can "Add as link" to add a link to a file in another project in the solution.

Is there any way to do this for entire folders, so that an entire folder in project A will be visible in project B, without the need to manually link to new items in that folder?

kpozin
  • 21,813
  • 17
  • 52
  • 71

6 Answers6

150

As this blogpost stated, it is possible.

<ItemGroup>
    <Compile Include="any_abs_or_rel_path\**\*.*">
        <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
    </Compile>
</ItemGroup>

But be aware, the files will not be copied.

Nick Strupat
  • 4,632
  • 4
  • 38
  • 54
mo.
  • 3,244
  • 1
  • 21
  • 20
  • 22
    +1. You could also use ``, and add `PreserveNewest` to make Visual Studio copy the resources on build. – Markus Jarderot May 18 '12 at 18:12
  • After adding this to my project, VS 2010 copied the referenced files into my project's folder. Totally NOT what I wanted. Is it possible to have it just add references without copying the files? – MindJuice Sep 21 '12 at 00:08
  • 9
    Try to use: `...` instead of `...`. But i think, it will copy those files anyway.Even VisualStudio does this with linked files. – mo. Sep 21 '12 at 11:33
  • 5
    @mo. your answer adds links to all files in the project root, sometimes it is inconvenient. %(RecursiveDir) should changed to some link folder name: for example to link source folder from wp7 project 'MyMainProject' in another project in this solution: engine\%(FileName) – Tertium Jan 31 '13 at 12:58
  • 1
    @Tertium Both methods have validity - but in response to the Question asked, the *entire* folder would be the recursive view. – The Evil Greebo Mar 05 '13 at 21:17
  • 25
    I had to use `%(RecursiveDir)%(FileName)%(Extension)` to prevent it from dropping the extensions off the file names. – Joe Daley Apr 15 '13 at 07:26
  • `Compile` seems like an odd choice for `*.*`. If you want to compile them all, you probably want `*.cs` or whatever VB.NET's extension is. – jpmc26 Jul 11 '16 at 18:09
  • 2
    For Icon Png Resources: – ChrisB Aug 12 '16 at 07:20
  • This works for shared project. Sometimes you want to include source code outside the shared project directory, which is not possible with Visual Studio itself. – zwcloud Jul 14 '17 at 07:28
  • I'd like to add that `any_abs_or_rel_path` should be a path to your directory. The other parts of the code stays as is. If you are developing a web app, `Compile` should be changed to `Content`. To copy those files while build, use MSBuild.WebApplication.CopyContentLinkedFiles NuGet Package. – Niko Apr 19 '18 at 07:50
127

In VS2012 and later, you can drag a folder to another project with alt key pressed. It's just the same as adding each file as link manually but faster.

upd: Consider using Shared Projects if you are using VS2013 update 2 (with Shared Project Reference Manager) or VS2015.

mt_serg
  • 7,097
  • 4
  • 24
  • 44
  • 1
    In VS2010, doing this will copy files and directories rather than add them as links, creating unwanted duplicates. – Tom Jan 20 '14 at 18:52
  • 3
    Note: the folder must be dragged from windows explorer (not another instance of visual studio). Also, it must be a left-click drag, not a right-click drag. Works great in VS2012. – davidpricedev Jan 31 '14 at 23:06
  • 11
    I think it just create links to every files in the sources folder, not a link to the folder itself. That means that if you add a file to the source folder, it will not be automatically linked. – Johnny5 Feb 26 '14 at 15:46
  • this did exactly what I was looking for. – Bruce Dunwiddie Sep 14 '14 at 17:15
  • The "alt-key" tricked worked, I had to modify my .csproj to get the files to be available on the web per [here](http://stackoverflow.com/a/16473399/1867157) to get the files to copy over. – DoubleJ May 31 '16 at 21:23
  • This is the better answer. One does not loose typescript compiler links as happens to files in nested folders when using the other suggested approaches. – MarzSocks Jul 06 '16 at 17:30
  • 1
    Dragging with Alt worked in VS 2017 (version 15.4.1) and saved the day since they removed Add As Link from the Add Existing File dialog. And no, it didn't create any duplicates. – Szybki Oct 30 '17 at 12:51
39

One addition to the answer from mo. and the comment from Marcus, if you are linking content items you will need to include the file extension:

<ItemGroup>
  <Compile Include="any_abs_or_rel_path\**\*.*">
    <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Compile>
</ItemGroup>
Pang
  • 8,605
  • 144
  • 77
  • 113
Chad
  • 509
  • 4
  • 3
26

Regarding the part of the original query to have a linked folder appear in the IDE, it is kind of possible to achieve this so there is a folder in the solution explorer with all linked files inside, instead of all the files appearing in the root of the solution. To achieve this, include the addition:

  <ItemGroup>
    <Compile Include="..\anypath\**\*.*">
      <Link>MyData\A\%(RecursiveDir)%(FileName)%(Extension)</Link>
    </Compile>
  </ItemGroup>

This will include all files from the linked directory in a new folder in the solution explorer called MyData. The 'A' in the code above can be called anything but must be there in order for the folder to appear.

Pang
  • 8,605
  • 144
  • 77
  • 113
Adam
  • 2,480
  • 1
  • 28
  • 29
9

If you want to add a folder as a reference and you don't want to compile it, use:

<Content Include="any_path\**\*.*">
  <Link>folder_in_B_project\%(RecursiveDir)%(FileName)%(Extension)</Link>
</Content>
Pang
  • 8,605
  • 144
  • 77
  • 113
Neshta
  • 2,277
  • 2
  • 24
  • 37
-2

Bust out the shell and add a symbolic link.

runas Administrator then

mklink /d LinkToDirectory DirectoryThatIsLinkedTo

BAM symbolic link!

/d specifies directory link.

Works in Vista on up out of the box. Can be backported to XP.

Documentation here: http://technet.microsoft.com/en-us/library/cc753194%28WS.10%29.aspx

For those not familiar with symbolic links, it's essentially a pointer to another file or directory. It's transparent to applications. One copy on disk, several ways to address it. You can also make a "hard link" which is not a pointer to another address, but an actual file ID entry in NTFS for the same file.

NOTE: as stated in the comments, this would only work on the computer where you created the symlink and wouldn't work in a Version Control System like git.

user276648
  • 5,199
  • 5
  • 51
  • 79
John Vance
  • 536
  • 4
  • 15
  • 2
    This would only be useful for a single developer (unless scripted). The other solutions form part of the shared source code so are more universally useful. – JRoughan Mar 08 '14 at 17:43
  • The question as asked was not about shared source code. That said, msysgit doesn't support symlinks, so bummer. – John Vance Mar 10 '14 at 01:51
  • 1
    This is not a filesystem question. Also please note this idea will drive _serious_ side effect with different source control systems and backup resore systems – g.pickardou Mar 27 '14 at 09:34
  • Apparently I'm not allowed to edit or delete my question. Your first objection is silly. The question was also not explicitly a project file editing question, so you should go downvote all those answers too. Your second objection is noted, and if I could edit my answer to add that caveat, I would. – John Vance Mar 31 '14 at 16:31