Skip to main content

Visual studio project version

Define a post-build to create a version file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <GeneratePackageOnBuild>False</GeneratePackageOnBuild>
    <Version>1.2.3</Version>
  </PropertyGroup>
  <Target Name="GetVersion" AfterTargets="PostBuildEvent">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="AssemblyInfo" />
    </GetAssemblyIdentity>
    <PropertyGroup>
      <VersionInfo>%(AssemblyInfo.Version)</VersionInfo>
    </PropertyGroup>
    <!--And use it after like any other variable:-->
    <Message Text="VersionInfo = $(VersionInfo)" Importance="high" />
  </Target>
  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <!-- 
    <Exec Command="call $(SolutionDir)BuildScripts\PostBuild.bat $(TargetPath) $(VersionInfo)" />
    -->
    <Exec Command="echo $(VersionInfo)&gt;$(TargetDir)\version.txt" />
  </Target>
</Project>