Visual Studio Installer not copying all files from publish directory

632 views Asked by At

I have an issue with the Visual Studio installer extension.
I need to copy files from a subfolder to the output root folder when creating an installer package. Since the installer project uses the "Publish Items from Projectname (Active)" output, I expect it to pack everything that will be output to the publish folder. I added a copy configuration to the .csproj file and it works when I am publishing to a folder. But the installer project doesn't include this file.

This is the test configuration.

  <Target Name="CopyTestDebug" AfterTargets="Publish" Condition="'$(Configuration)'=='Debug'">
    <ItemGroup>
      <_CopyTestDebug Include="Folder\Test_Debug.data" />
    </ItemGroup>
    <Copy SourceFiles="@(_CopyTestDebug)" DestinationFiles="$(PublishDir)Test.data" />
  </Target>

  <Target Name="CopyTestRelease" AfterTargets="Publish" Condition="'$(Configuration)'=='Release'">
    <ItemGroup>
      <_CopyTestRelease Include="Folder\Test_Release.data" />
    </ItemGroup>
    <Copy SourceFiles="@(_CopyTestRelease)" DestinationFiles="$(PublishDir)Test.data" />
  </Target>

When I publish this the file "Test.data" is in the root folder of the publish directory (as expected).

But when observing the output of "Publish Items from Project (Active)" at the Visual Studio installer project, the file "Test.data" is missing.

I tried hacks like:

  <Target Name="CopyTest2" AfterTargets="CopyTest">
    <ItemGroup>
      <Content Include="$(PublishDir)Test.data" CopyToOutputDirectory="Always"/>
    </ItemGroup>
  </Target>

But the target path of the file is not the root folder but includes the relative path of the publish folder (e.g. "bin\Release.net6.0-windows\win-x64\publish\Test.data" instead of "Test.data"), which makes sense.

The only hack that works is something like:

  <Target Name="CopyTest" BeforeTargets="BeforeBuild">
    <ItemGroup>
      <_CopyTest Include="Folder\Test.data" />
    </ItemGroup>
    <Copy SourceFiles="@(_CopyTest)" DestinationFiles="Test.data" />
  </Target>

  <ItemGroup>
      <Content Include="Test.data" CopyToOutputDirectory="Always"/>
  </ItemGroup>

Instead of copying to the publish directory it will be copied to the root folder of the project and therefore the Visual Studio installer doesn't mess up with the paths, but this solution is not ideal, because it pollutes the project folder.

1

There are 1 answers

1
Bowman Zhu-MSFT On

I tested the updated content you provided, and it works with no problem.

But since you don't want pollute the project folder, why not using the content like this?

<ItemGroup>
  <Content Include="Folder\Test.data" CopyToPublishDirectory="Always"/>
</ItemGroup>

It also works and it is very short and easy:

enter image description here

You can refer to this official document:

CopyToPublishDirectory

I think it is not related to target before or after in this situation. If I misunderstand something, please feel free to let me know.