What is the msbuild property containing the name of a .Net self-contained single-file application accessible to a target that runs after the publish event?

RLWA32 52,811 Reputation points
2026-07-20T16:46:25.6933333+00:00

I want to use a Copy task after a self-contained single-file application is published. I added a target to the .csproj containing a Copy task. I know that the published binary is written to the location pointed to by the $(PublishDir) property. At the moment I am using $(Assembly).exe to refer to the binary.

This is what is currently present in the .csproj file to implement the post-publish copy -

  <ItemGroup>
    <SrcFile Include="$(PublishDir)$(AssemblyName).exe" />
    <DestFolder Include="C:\Users\RLWA32\source\repos\RlwA32\Win32SingleTest\Win32SingleTest\" />
  </ItemGroup>

  <Target Name="CopyToWin32" AfterTargets="Publish">
    <Message Importance="high" Text="CopyToWin32 Src: @(SrcFile) Dst: @(DestFolder)" />
    <Copy SkipUnchangedFiles='true' SourceFiles="@(SrcFile)" DestinationFolder="@(DestFolder)" />
  </Target>

Is there an msbuild property that directly refers to the binary so that it can be used for the Copy task?.

Developer technologies | .NET | .NET CLI

Answer accepted by question author
Danny Nguyen (WICLOUD CORPORATION) 8,620 Reputation points Microsoft External Staff Moderator
2026-07-21T08:57:53.4566667+00:00

Hi @RLWA32

There is an MSBuild property that directly contains the path to the final single-file binary. You can use the $(PublishedSingleFilePath) property.

I have tested this and it works as expected. This property is populated during the Publish target specifically when creating a single-file bundle.

Because this property is created during the publish process, it will be empty if you define your <ItemGroup> at the top level of your project. You need to move your <ItemGroup> inside your target so MSBuild evaluates it after the publish process is complete and the property has a value.

Here is a general example of how to use it for a copy task:

  <!-- Define the target to run after publish -->
  <Target Name="MyPostPublishCopy" AfterTargets="Publish">

    <!-- The ItemGroup must be inside the target to get the populated value -->
    <ItemGroup>
      <MySourceFile Include="$(PublishedSingleFilePath)" />
      <MyDestFolder Include="C:\MyDestinationFolder\" />
    </ItemGroup>

    <Copy SkipUnchangedFiles="true" 
          SourceFiles="@(MySourceFile)" 
          DestinationFolder="@(MyDestFolder)" />

  </Target>

By doing this, you will dynamically get the exact path and filename of the single-file application, regardless of the operating system or runtime identifier.

For more information on single-file deployments, you can refer to the official documentation on creating a single file for application deployment.If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide feedback.

Thank you.

Was this answer helpful?

2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.