154

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?

8 Answers 8

171

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.

10
  • 24
    +1. You could also use <Content ...>, and add <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> to make Visual Studio copy the resources on build. May 18, 2012 at 18:12
  • 11
    Try to use: <None>...</None> instead of <Compile>...</Compile>. But i think, it will copy those files anyway.Even VisualStudio does this with linked files.
    – mo.
    Sep 21, 2012 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: <ItemGroup> <Compile Include="..\..\MyMainProject\MyMainProject\engine*.*"> <Link>engine\%(FileName)</Link> </Compile> </ItemGroup>
    – Tertium
    Jan 31, 2013 at 12:58
  • 25
    I had to use <Link>%(RecursiveDir)%(FileName)%(Extension)</Link> to prevent it from dropping the extensions off the file names.
    – Joe Daley
    Apr 15, 2013 at 7:26
  • 2
    For Icon Png Resources: <EmbeddedResource Include="..\..\Icons\16*.png">
    – ChrisB
    Aug 12, 2016 at 7:20
129

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.

7
  • 1
    In VS2010, doing this will copy files and directories rather than add them as links, creating unwanted duplicates.
    – Tom
    Jan 20, 2014 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. Jan 31, 2014 at 23:06
  • 13
    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, 2014 at 15:46
  • this did exactly what I was looking for. Sep 14, 2014 at 17:15
  • 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, 2017 at 12:51
44

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>
1
  • whats this <Compile ... in this. In my case, i need to refer .ts files to my new project. how its done. thanks! Jan 20, 2015 at 9:55
30

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.

3
  • it seems to do the trick but doesn't fold Form files into one, and gives errors for their .resx files (I'm using VB). Thanks anyway. Nov 25, 2015 at 9:49
  • 1
    @IvanFerrerVilla, yes it has some problems I've noticed, but for the most part is only good for the looks.
    – Adam
    Nov 26, 2015 at 2:52
  • 3
    With Visual Studio 2015, this worked for me even without the `A`.
    – Martin
    Aug 5, 2016 at 11:28
13

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>
2

Even when there are so many solutions it took me a while to understand it. Here I will try to explain it a little bit more.

I needed link to the whole folder so my final result is:

<ItemGroup>
    <Content Include="..\Gym.Management.Api\TestFolder\**\*.*">
        <Link>TestFolder\%(RecursiveDir)%(FileName)%(Extension)</Link>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>

where:

  1. ..\Gym.Management.Api\TestFolder\ represents path to the other project containing the folder I want to link
  2. TestFolder\ in <link> tag is the final(destination) folder in my current project where I want to link it

TIP: When you are not sure how to get the proper Include path then in your current project right click on project->click Add->Existing item->navigate to one of those files from folder you want to link-> instead of Add, press the dropdown arrow next to it->click Add as link. This link is inserted in your .csproj file and from there you can extract the Include path.

1
  • Works, but the TIP is the best! Oct 27, 2023 at 11:00
0

If what you are looking for is to add another folder to your current workspace for easy access & development experience.

you can do this by just clicking file -> Add Folder to Workspace option in vscode.

-3

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.

4
  • 3
    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 8, 2014 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, 2014 at 1:51
  • 2
    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 Mar 27, 2014 at 9: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, 2014 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.