Wednesday, December 1, 2010

Versioning assembly at build

Method 1 


open up the AssemblyInfo.cs file and change
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
to
[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]
you can do this in IDE by going to project -> properties -> assembly information
This however will only allow you to auto increment the Assembly version and will give you the"Assembly File Version: A wildcard ("*") is not allowed in this feild" message box if you try place a * in the file version field.
So just open up the assemblyinfo.cs and do it manually.


Method 2

Another option for changing version numbers in each build is to use the Version task ofMSBuild.Community.Tasks. Just download their installer, install it, then adapt the following code and paste it after  in your.csproj file:
 Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
 Name="BeforeBuild">
     VersionFile="Properties\version.txt" Major="1" Minor="0" BuildType="Automatic" StartDate="12/31/2009" RevisionType="BuildIncrement">
       TaskParameter="Major" PropertyName="Major" />
       TaskParameter="Minor" PropertyName="Minor" />
       TaskParameter="Build" PropertyName="Build" />
       TaskParameter="Revision" PropertyName="Revision" />
    
     CodeLanguage="CS"
                  OutputFile="Properties\VersionInfo.cs"
                  AssemblyVersion="$(Major).$(Minor)"
                  AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />

Note: Adapt the StartDate property to your locale. It currently does not use the invariant culture.
For the third build on January 14th, 2010, this creates a VersionInfo.cs with this content:
[assembly: AssemblyVersion("1.0")]
[assembly: AssemblyFileVersion("1.0.14.2")]
This file then has to be added to the project (via Add existing item), and the AssemblyVersion andAssemblyFileVersion lines have to be removed from AssemblyInfo.cs.
The different algorithms for changing the version components are described in$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.chm andVersion Properties.

Note
As of right now, for my application,
string ver = Application.ProductVersion;
returns
ver = 1.0.3251.27860
The value 3251 is the number of days since 1/1/2000. I use it to put a version creation date on the splash screen of my application. When dealing with a user, I can ask the creation date which is easier to communicate than some long number.
(I'm a one-man dept supporting a small company. This approach may not work for you.)

No comments:

Post a Comment