User Tools

Site Tools

blog:2024-01-31_c_build_execution_file_with_version_number_name



2024-01-31 C#: Build Execution File Name with Version Number

  • We need to build the target execution file with the build version file name

Method

  • Close the project
  • Edit the project.csproj by text editor and append this code piece on the end of XML file </project> tage
  •   <Target Name="PostBuildMacros">
        <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
          <Output TaskParameter="Assemblies" ItemName="Targets" />
        </GetAssemblyIdentity>
        <ItemGroup>
          <VersionNumber Include="@(Targets->'%(Version)')" />
        </ItemGroup>
      </Target>
      <PropertyGroup>
        <PostBuildEventDependsOn>
        $(PostBuildEventDependsOn);
        PostBuildMacros;
      </PostBuildEventDependsOn>
        <PostBuildEvent>echo The Assembly Version is: @(VersionNumber)
    copy /Y "$(TargetDir)$(TargetFileName)" "$(TargetDir)$(TargetName)_@(VersionNumber).exe"
    </PostBuildEvent>
      </PropertyGroup>
      <PropertyGroup>
        <PreBuildEvent>del /q "$(TargetDir)$(TargetName)_*.exe"</PreBuildEvent>
      </PropertyGroup>
  • This snippet has an example <PostBuildEvent> already in it. No worries, you can reset it to your real post-build event after you have re-loaded the project.
  • Now, the assembly version is available to your post-build event with this macro:
    @(VersionNumber)
  • Reopen the project with Visual Studio, then you can get it

Reference

  • Link - del (MS Learn)
  • Link - copy (MS Learn)
  • Link - [cmd] XCOPY備份功能
  • Link - Determine assembly version during a post-build event (stack overflow)
  • Link - Renaming project output in visual studio using post build event generates error (stack overflow)
  • Link - Visual Studio 專案建置完成後取得組件版本號並輸出在文字檔中

TAGS

  • 56 person(s) visited this page until now.

Permalink blog/2024-01-31_c_build_execution_file_with_version_number_name.txt · Last modified: 2024/01/31 15:02 by jethro

oeffentlich