kpozin
kpozin

Reputation: 26969

"Add as Link" for folders in Visual Studio projects

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?

Upvotes: 155

Views: 80645

Answers (8)

mo.
mo.

Reputation: 3544

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.

Upvotes: 174

Edward795
Edward795

Reputation: 91

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.

Upvotes: 0

Kebechet
Kebechet

Reputation: 2387

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.

Upvotes: 3

Neshta
Neshta

Reputation: 2745

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>

Upvotes: 13

Adam
Adam

Reputation: 2802

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.

Upvotes: 31

Chad
Chad

Reputation: 574

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>

Upvotes: 44

John Vance
John Vance

Reputation: 532

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.

Upvotes: -3

s.matushonok
s.matushonok

Reputation: 7585

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.

Upvotes: 127

Related Questions