msbuild

MSBuild: CopyToOutputDirectory copies a folder when a file path is specified


A subfolder of the project folder contains a library file that needs to be copied to the output folder. Here's how I tried to specify the copy.

<ItemGroup>
    <Content Include="MyFolder\my_lib.dll">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>

Expected result: The required file is copied to the output folder.
Actual result: The required file's parent folder, including the required file, is copied.

Is this a bug?
And how can I implement the requirement?.


Solution

  • The MSBuild Copy task "[maintains] the directory structure" when it copies files. This is by design and is not a bug.

    But the Content item has a TargetPath metadata that can be used to override and set a destination path.