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?
<EmbeddedResource Include="..\..\..\Demo\*.cs" Link="Resources\*.cs" />
<EmbeddedResource Include="..\..\..\Demo\*.cs" Link="Resources\*" />
<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?
To add a file as a link, right click and choose Add > Existing Item… as before, but this time, don't click the Add button. Instead, click the little dropdown arrow next to the Add button and select Add as Link. Instead of copying the file into the project directory, Visual Studio will create a link to the original.
Developers compile CSPROJ files using MSBuild (the Microsoft Build Engine). When developers create a new program in Microsoft Visual Studio, they start by creating a new project and associated project file.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With