“Add as Link” for folders in Visual Studio project

2019-01-12 16:44发布

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?

7条回答
萌系小妹纸
2楼-- · 2019-01-12 16:55

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.

查看更多
Explosion°爆炸
3楼-- · 2019-01-12 16:57

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.

查看更多
在下西门庆
4楼-- · 2019-01-12 17:00

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>

Sorry for the extra answer, I don't have enough reputation to add a comment.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-12 17:11

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.

查看更多
女痞
6楼-- · 2019-01-12 17:12

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>
查看更多
爷、活的狠高调
7楼-- · 2019-01-12 17:16

There's no way to do it for entire folders, but if two projects are in the same solution you can use VSCommands 2010 to copy several files as links at the same time. see this video for more info http://www.youtube.com/watch?v=fa1M0NGXqMg or go to visualstudiogallery.msdn.microsoft.com/en-us/d491911d-97f3-4cf6-87b0-6a2882120acf?lc=1033

查看更多
登录 后发表回答