MSBuild SetVersion Task

This is a simple MSBuild task allowing you to set or update the assembly version of a C# file


<SetVersion FileName="AssemblyInfo.cs" AssemblyVersion="1.2.+.=" />

The rule "1.2.+.=" means: set the Major version to 1, set the Minor version to 2, increment the Build and leave the Revision at whatever it was before. The AssemblyVerision will be changed - all other attributes will be left unchanged.


Here's a more complete example. We store the location of SetVersionTask.dll in a PropertyGroup. We've got an UpdateVersionNumber target that only runs if it is a release build and if SetVersionTask.dll can be found. Then we increment the build number in for both AssemblyVersion and AssemblyFileVersion. Since this example is in a csproj file, for our target to be automatically be called before a Release build from Visual Studio, we make the built-in "BeforeBuild" Target depend on our UpdateVersionNumber target.

<UsingTask TaskName="SetVersion" AssemblyFile="$(SetVersionPath)" />
<Target Name="UpdateVersionNumber" Condition="Exists('$(SetVersionPath)') AND '$(Configuration)' == 'Release'">
  <Message Text="Updating Version..." />
  <SetVersion FileName="Properties\AssemblyInfo.cs" AssemblyVersion="=.=.+.=" AssemblyFileVersion="=.=.+.=" />
<Target Name="BeforeBuild" DependsOnTargets="UpdateVersionNumber">  

Note: If you are compiling from within Visual Studio and have the AssemblyInfo.cs file open, then although AssemblyInfo.cs will get updated, the build will use the version of AssemblyInfo.cs as it was when you launched compilation. The workaround is to leave AssemblyInfo.cs closed when building, or build from the command line using msbuild. If I can find a fix to this I will include it in the next version. Another workaround is described here