"Add as Link" for folders in Visual Studio projects
Asked Answered
W

8

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?

Windrow answered 31/8, 2010 at 15:58 Comment(0)
B
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.

Byway answered 28/2, 2012 at 16:42 Comment(10)
+1. You could also use <Content ...>, and add <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> to make Visual Studio copy the resources on build.Curtcurtail
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?Freemason
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.Byway
@Byway 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>Antic
@Antic Both methods have validity - but in response to the Question asked, the entire folder would be the recursive view.Hyperacidity
I had to use <Link>%(RecursiveDir)%(FileName)%(Extension)</Link> to prevent it from dropping the extensions off the file names.Domenech
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.Concerning
For Icon Png Resources: <EmbeddedResource Include="..\..\Icons\16*.png">Lebeau
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.Cannibalism
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.Continental
T
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.

Tristantristas answered 14/3, 2013 at 8:57 Comment(7)
In VS2010, doing this will copy files and directories rather than add them as links, creating unwanted duplicates.Juristic
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.Horseradish
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.Bight
this did exactly what I was looking for.Tumult
The "alt-key" tricked worked, I had to modify my .csproj to get the files to be available on the web per here to get the files to copy over.Aylmer
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.Navarro
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.Lacker
P
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>
Phalanx answered 12/1, 2013 at 17:54 Comment(1)
whats this <Compile ... in this. In my case, i need to refer .ts files to my new project. how its done. thanks!Globule
P
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.

Polder answered 14/1, 2013 at 7:55 Comment(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.Sat
@IvanFerrerVilla, yes it has some problems I've noticed, but for the most part is only good for the looks.Polder
With Visual Studio 2015, this worked for me even without the `A`.Corroborant
L
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>
Learn answered 13/2, 2015 at 13:48 Comment(0)
V
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.

Vinnie answered 22/6, 2022 at 11:25 Comment(1)
Works, but the TIP is the best!Virge
B
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.

Bismuth answered 21/1, 2023 at 12:36 Comment(0)
H
-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.

Hymenium answered 20/2, 2014 at 22:22 Comment(4)
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.Charlsiecharlton
The question as asked was not about shared source code. That said, msysgit doesn't support symlinks, so bummer.Hymenium
This is not a filesystem question. Also please note this idea will drive serious side effect with different source control systems and backup resore systemsJoly
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.Hymenium

© 2022 - 2024 — McMap. All rights reserved.