New .csproj format - How to specify entire directory as “linked file” to a subdirectory?

早过忘川 提交于 2019-12-18 04:01:53

问题


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?


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/45800697/new-csproj-format-how-to-specify-entire-directory-as-linked-file-to-a-subdi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!