Closed yhvicey closed 6 years ago
The 2.0 version of the .NET SDK (visual studio 15.4+, .NET CLI 2.0.0+) has a feature that can be used for this: the LinkBase
metadata:
<ItemGroup>
<None Include="$(SolutionDir)config\**"
CopyToOutputDirectory="PreserveNewest"
LinkBase="config\" />
</ItemGroup>
Depending on which project type you have, the None
items may already exist - e.g. for .net standard and .net core projects (non-web projects, web projects would use Content
for .json
files) and you could just update their CopyToOutputDirectory
metadata:
<ItemGroup>
<None Update="$(SolutionDir)config\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
If you need to do this in non-.net standard/core projects (non-"SDK" projects), you can use the Link
metadata:
<ItemGroup>
<Content Include="..\sql\**" CopyToPublishDirectory="PreserveNewest" Link="sql\%(RecursiveDir)\%(Filename)%(Extension)" />
</ItemGroup>
It works. Thank you!
@dasMulli How can we specify the target folder path
in the output directory ? Like final it should go in abc/def
folder under output directory. This is for .net core projects.
@ashishnegi if you're using this code, you can put the target folder into the LinkBase
metadata attribute.
I had to change <None Update
to <None Include
to make the copy work.
@dasMulli I have following structure:
MyProject.csproj
---Components
------A.dll
------B.dll
---------NestedDir
------------X.dll
------------Y.dll
How to remove the Components
directory and put its content to output directory?
Using this code copy Components
folder into output directory.
I want this (in output directory):
MyProject.dll
---A.dll
---B.dll
------NestedDir
---------X.dll
---------Y.dll
Use the same code without LinkBase
(or set it to \
) and add
<PropertyGroup>
<DefaultItemExcludes>$(DefaultItemsExclude);Components\**\*</DefaultItemExcludes>
</PropertyGroup>
So there won't be conflicting items with different copy metadata.
@dasMulli I got same result. I want to copy Components
's content into Output
. I don't want to copy Components
folder, just its content.
from https://stackoverflow.com/a/35065306/492
<ContentWithTargetPath Include="lib\some_file.dat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>some_file.dat</TargetPath>
</ContentWithTargetPath>
may be worth trying with wildcards. I haven't tried it with wildcards, works fine for just one file.
this worked for me
<ContentWithTargetPath
Include="$(MSBuildThisFileDirectory)..\Files\*.*"
CopyToOutputDirectory="PreserveNewest"
TargetPath="Files\%(Filename)%(Extension)" />
Please note that using ContentWithTargetPath currently breaks incremental builds (As in, it will always build the project, even when nothing has changed).
It seems to check the default target path for your file, instead of checking the specified TargetPath, and when the file isn't found at the default path, it concludes that the file is missing and the project must be built again.
If you need to do this in non-.net standard/core projects (non-"SDK" projects), you can use the
Link
metadata:<ItemGroup> <Content Include="..\sql\**" CopyToPublishDirectory="PreserveNewest" Link="sql\%(RecursiveDir)\%(Filename)%(Extension)" /> </ItemGroup>
It seems that this is not working in razor projects.
thanks, the content include is work for me.
I am using .net core 5. please check out the below code if you need it.
the startup project is CoreSystemConsole, I want the puppeteer project to copy a templates folder to the build/debug directory under CoreSystemConsole on the program run.
kindly take the whole solution from here: https://github.com/KeithPoonNS/ReportEngine
my startup project
<ItemGroup>
<None Update="ReportTemplate\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<!--<None Update="$(SolutionDir)PuppeteerReport\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>-->
<Content Include="$(SolutionDir)PuppeteerReport\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Update="..\OfficeToPDF-1.9.0.2\OfficeToPDF.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
the puppeteer project
<ItemGroup>
<None Update="ReportTemplate\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<!--<None Update="$(SolutionDir)PuppeteerReport\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>-->
<Content Include="$(SolutionDir)PuppeteerReport\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
The 2.0 version of the .NET SDK (visual studio 15.4+, .NET CLI 2.0.0+) has a feature that can be used for this: the
LinkBase
metadata:<ItemGroup> <None Include="$(SolutionDir)config\**" CopyToOutputDirectory="PreserveNewest" LinkBase="config\" /> </ItemGroup>
Saved my day! Thanks, @dasMulli !
@dasMulli how to copy output including folders within it to different path ? what i do is copying after build
<Target Name="CopyFiles" AfterTargets="CoreBuild">
<ItemGroup>
<RootItem Include="$(ProjectDir)*.addin" />
<AddinItem Include="$(TargetDir)*/*.*" />
</ItemGroup>
<PropertyGroup>
<RootDir>bin\$(SharingType) $(RevitVersion) $(Configuration)\</RootDir>
<AddinDir>$(RootDir)$(AssemblyName)\</AddinDir>
</PropertyGroup>
<Copy SourceFiles="@(RootItem)" DestinationFolder="$(RootDir)" />
<Copy SourceFiles="@(AddinItem)" DestinationFolder="$(AddinDir)" />
<ItemGroup>
<AddinFiles Include="$(RootDir)**\*.*" />
</ItemGroup>
<Copy SourceFiles="@(AddinFiles)"
DestinationFolder="$(AppData)\Autodesk\Revit\Addins\$(RevitVersion)\%(RecursiveDir)" />
</Target>
so what happens is my bin is like this
|-- file1.txt |-- file2.txt |-- subfolder1\ | |-- subfolder2\ | | |-- file3.txt | | |-- file4.txt |-- subfolder2\ |-- file5.txt
but after copying to my path, it copies all files but with no folders ! it will be like this
|-- file1.txt |-- file2.txt |-- file3.txt |-- file4.txt |-- file5.txt
@AmeerMansourWSP, Example 2 in Copy Task documentation shows one way to copy the directory structure, by using %(RecursiveDir)
in the DestinationFolder parameter.
Came from https://github.com/dotnet/project-system/issues/3203
Following steps in @rainersigwald's post, files did have been copied to output folder, but the root folder ("config\" itself) can not be copy to output path. For example:
Is there any way to keep its root folder too?
Here's the code snippet: