peteris.rocks

Versioning with MSBuild

Last updated on

Use Semantic Versioning (semver) http://semver.org/

.NET AssemblyVersion etc. http://stackoverflow.com/questions/4466019/fileversioninfo-and-assemblyinfo

AssemblyInfo

Edit AssemblyInfo.cs file in your project.

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Tilde.Namespace.Project")]
[assembly: AssemblyDescription("Project")]
[assembly: AssemblyCompany("Tilde")]
[assembly: AssemblyProduct("Tilde.Namespace")]
[assembly: AssemblyCopyright("Copyright © Tilde")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: ComVisible(false)]

Delete this file from test projects so that you don't have to maintain it.

MSBuild

Define versions in <Project>

<PropertyGroup>
  <Version>1.0.0-dev</Version>
  <AssemblyVersion>1.0</AssemblyVersion>
  <AssemblyFileVersion>$(AssemblyVersion).*.*</AssemblyFileVersion>
</PropertyGroup>

Add this in <Project>

<Target Name="BeforeBuild">
  <ItemGroup>
    <VersionInfo Include="[assembly: System.Reflection.AssemblyVersion(&quot;$(AssemblyVersion)&quot;)] // Generated by build" />
    <VersionInfo Include="[assembly: System.Reflection.AssemblyFileVersion(&quot;$(AssemblyFileVersion)&quot;)] // Generated by build" />
    <VersionInfo Include="[assembly: System.Reflection.AssemblyInformationalVersion(&quot;$(Version)&quot;)] // Generated by build" />
    <VersionInfo Include="[assembly: System.Reflection.AssemblyConfiguration(&quot;$(Configuration)&quot;)] // Generated by build" />
  </ItemGroup>
  <MakeDir Directories="$(IntermediateOutputPath)" />
  <WriteLinesToFile File="$(IntermediateOutputPath)VersionInfo.cs" Overwrite="True" Lines="@(VersionInfo)" />
  <ItemGroup>
    <Compile Include="$(IntermediateOutputPath)VersionInfo.cs" />
  </ItemGroup>
</Target>

MSBuild Community Tasks

Another alternative is using MSBuild Community Tasks https://github.com/loresoft/msbuildtasks

Code

You can get the version

FileVersionInfo info = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
string version = info.FileVersion;

Shared versions

You could also have PropertyGroup with Version in a separate file and then import it in all projects and then you'll have the same versions in all your assemblies.

Or create GlobalAssemblyInfo.cs in your solution and add it as a link to your projects.