24

With the new .csproj format (as well as the old), it is possible to add files as linked outside of the project folder:

 <EmbeddedResource Include="..\..\..\Demo\Sample.cs" Link="Resources\Sample.cs" />

It is also possible to use a glob pattern to include multiple files:

<EmbeddedResource Include="..\..\..\Demo\*.cs" />

But how do you combine the two?

What I Tried

  1. <EmbeddedResource Include="..\..\..\Demo\*.cs" Link="Resources\*.cs" />
  2. <EmbeddedResource Include="..\..\..\Demo\*.cs" Link="Resources\*" />
  3. <EmbeddedResource Include="..\..\..\Demo\*.cs" Link="Resources\" />

The first two only create a single linked file (with exactly the name *.cs and * respectively). The third simply errors out.

Is there a way to combine globbing with linked files to a specific location in the target project? If not, how can I link all the files in a directory without knowing how many or what their names are?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212

2 Answers2

36

While this was previously possible using the %(RecursiveDir) metadata when using glob expansion ( Link="Resources\%(RecursiveDir)%(Filename)%(Extension)"), the 2.0.0 version of the .NET Core SDK allows the use of a new LinkBase metadata:

<EmbeddedResource Include="..\..\..\Demo\**\*.cs" LinkBase="Resources" />

Note that you need to install the 2.0.0 in addition to the recently released VS 2017 15.3 (and ensure no global.json selects a lower version).

It was introduced with this pull request which is probably the best documentation at the moment.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • How would I specify the `MinimumVisualStudioVersion` to enforce this? I tried using [this script](https://blogs.msdn.microsoft.com/dmx/2017/06/13/how-to-get-visual-studio-2017-version-number-and-edition/) to lookup my version (`15.3.26730.12`), but when I put this value into the solution file, VS 2017 15.3 doesn't open it. Also tried 15.3, but that doesn't work either. This seems like a reasonable thing to add to this answer given the minimum requirement of 15.3. Is there a list of these version numbers somewhere? – NightOwl888 Sep 10 '17 at 22:36
  • 1
    Not really possible at the moment. The problem is you don't just need 15.3, you also need the 2.0.0 SDK (which isn't included in 15.3). https://github.com/Microsoft/msbuild/issues/2350 – Martin Ullrich Sep 11 '17 at 03:17
  • .NET Core SDK 2.x is included in later versions of VS such as 15.7.2 so you may get a suitable .NET Core SDK version but applying the latest VS 2017 update, provided that is an option for you. - @martin-ullrich you just saved us a large amount of work/risk! Thank you! – Manfred May 29 '18 at 03:05
5

I got this working for me (linking all svg-files in an external dir to a solution-subfolder) with a hint from this site. Only the %(Parent.Filename) didn't work for me (got a CS1508), so I replaced with %(Filename)%(Extension).

<ItemGroup>
    <Parent Include="C:\Path\To\My\SVG\Dir\*.svg" />
    <EmbeddedResource Include="@(Parent)">
        <Link>Resources\%(Filename)%(Extension)</Link>
    </EmbeddedResource>
</ItemGroup>
freakinpenguin
  • 831
  • 14
  • 24