Can I use both wildcard and Link element inside the Compile element?

<Content Include="..\..\SomeDirectory\**\*.xml">
  <Link>SomeLinkDirectoryOfYourChoosing\%(Filename)%(Extension)</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

To include subfolders:

<ItemGroup>
  <Compile Include="..\SomeExternalFolder\**\*.cs" LinkBase="YourProjectFolder" />
</ItemGroup>

Others have suggested the using the Link attribute with placeholders, which indeed works. However, Microsoft has implemented a new attribute (which isn't in any of my code completion suggestions), named LinkBase, shown below.

<ItemGroup>
   <Compile Include="..\SomeDirectory\*.cs" LinkBase="SomeDirectoryOfYourChoosing" />
</ItemGroup>

Sources:

  • https://github.com/Microsoft/msbuild/issues/2795
  • https://github.com/dotnet/sdk/pull/1246
  • Link Additional Files in Visual Studio

For the sake of others, here's the answer plus the comment from Dean and Miserable Variable, which I found useful:

I have two projects, and I need to include *.xsd from one in the other, without copying the files or having to update the referencing csproj file every time a new XSD is added to the first.

The solution was to add the following to the csproj file

  <Content Include="..\BusinessLayer\Schemas\*.xsd">
    <Link>Contract\Schemas\xxx.xsd</Link>
  </Content>

Note xxx.xsd, you have to give a dummy filename in the Link element. It just gets replaced.

Also, you can include all sub folders with:

  <Content Include="..\BusinessLayer\Schemas\**\*.xsd">
    <Link>Contract\Schemas\ThisTextDoesntMatter</Link>
  </Content>

And files of all type (useful for pulling in CSS/JS/Style folders from 3rd parties) with:

  <Content Include="..\PresentationLayer\CustomerStyle\**\*.*">
    <Link>CustomerStyle\placeHolder</Link>
  </Content>

Tags:

Msbuild

Csproj