我可以使用"copy if newer"属性将文件放入bin目录。但问题是,我需要在我的应用程序旁边放置大量的dll文件,这意味着我的项目变得杂乱无章,文件数量也很多。我想把文件放进解决方案的资产文件夹中,在构建时让文件出现在bin目录中。我尝试过使用后构建事件,但即使成功,我仍然会收到Windows错误代码。是否有其他方法让外部dll文件可供我的应用程序使用呢?
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
-->
让我们进行修改。
首先定义Items,AfterBuild任务将要复制哪些文件。注意,您只需要确保这些文件在磁盘上的文件夹(您称之为assets)中存在并且受源代码控制。您不需要在解决方案或项目中包含任何这些文件。
<ItemGroup>
<!-- Include relative to this project file, so ..\assets would bring you to the solution folder
Take all files in the assets folder and subfolders, except *.txt files
-->
<Asset Include="assets\**" Exclude="*.txt">
</Asset>
<!-- take all *.txt files -->
<TextAsset Include="assets\**\*.txt">
<!-- meta data -->
<SubPath>TextFiles</SubPath>
</TextAsset>
</ItemGroup>
<!-- this does what the name suggests-->
<Target Name="AfterBuild">
<!-- log -->
<Message Importance="high" Text="Start Copying assets"/>
<!-- copy Asset files to one folder (flattens) -->
<Copy SourceFiles="@(Asset)"
DestinationFolder="$(OutputPath)" />
<!-- copy TextAsset files to a subpath, keep folder structure-->
<Copy SourceFiles="@(TextAsset)"
DestinationFiles="@(TextAsset->'$(OutputPath)%(SubPath)\%(RecursiveDir)%(Filename)%(Extension)')" />
<!-- done logging -->
<Message Importance="high" Text="Copied assets"/>
</Target>
$(OutputPath)
,这是众所周知的属性之一。类似的列表也适用于项目元数据。msbuild Application2.csproj /t:AfterBuild