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 targer.

<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.


Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.