且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用CSPROJ dotnetcore将文件复制到输出目录

更新时间:2022-03-01 01:10:44

有很多方法可以实现您的目标,具体取决于您的需求

There's quite a few ways to achieve your goals, depending on what your needs are.

最简单的方法是设置元数据( CopyToOutputDirectory / CopyToPublishDirectory )项目(假设 .txt None 项目而不是 Content) ,如果不起作用,请尝试使用< Content> 代替):

The easiest approach is setting the metadata (CopyToOutputDirectory / CopyToPublishDirectory) items conditionally (assuming .txt being a None item instead of Content, if it doesn't work, try <Content> instead):

<ItemGroup Condition="'$(Configuration)' == 'Debug'">
  <None Update="foo.txt" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

如果需要更多控制,最通用的方法是添加可挂接到构建过程中的自定义目标在csproj文件中:

If more control is required, the most versatile approach is to add custom targets that hooks into the build process in the csproj file:

<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
  <Copy SourceFiles="foo.txt" DestinationFolder="$(OutDir)" />
</Target>
<Target Name="CopyCustomContentOnPublish" AfterTargets="Publish">
  <Copy SourceFiles="foo.txt" DestinationFolder="$(PublishDir)" />
</Target>

这会将文件复制到相应目录。有关< Copy> 任务的更多选项,请参见其文档。要将其限制为某些配置,可以使用 Condition 属性:

This copies a file to the respective directories. For more options for the <Copy> task see its documentation. To limit this to certain configurations, you can use a Condition attribute:

<Target … Condition=" '$(Configuration)' == 'Release' ">

Condition 属性可以同时应用于< Target> 元素或诸如< Copy> 之类的任务元素。

This Condition attribute can be applied both on the <Target> element or on task elements like <Copy>.