Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use release date as assembly version for a .net project?

Tags:

c#

.net

I know that the format of the assembly version is:

<major version>.<minor version>.<build number>.<revision>

Is there a way to make the version number the current date?

For example, if I compile the build today, the version number should look like this:

2016.02.11.xxxxx

Where xxxxx is what you normally get if you set the assembly version to 1.0.0.*.

I googled around but didn't find an answer (not even a question) for this.

like image 976
Allen Zhang Avatar asked Feb 10 '16 20:02

Allen Zhang


2 Answers

Easy part: In Project Properties > Build Events, add a "Pre-build event command line" like this:

"D:\SomePath\MyAssemblyInfoPatcher.exe" "$(ProjectDir)Properties\AssemblyInfo.cs"

Hard part: You provide MyAssemblyInfoPatcher.exe , a program which opens the file specified by its first argument, searches it for this line (ignoring the values there):

[assembly: AssemblyVersion("8888.0.*")]

, replaces the line with one of these (or similar):

[assembly: AssemblyVersion("2016.11.05.2359")]
[assembly: AssemblyVersion("2016.1105.2359.1")]
[assembly: AssemblyVersion("8888.2016.1105.2359")]

, and rewrites the file.

The program derives these values from the system date and time according to a pattern like one of these:

yyyy.mm.dd.hhmm
yyyy.mmdd.hhmm.counter
arbitrary.yyyy.mmdd.hhmm

If you hard-code the pattern you want, your program needs no configuration. For the counter (if you chose it), the program can increment the value it finds in the file. The counter must be word (UInt16) so that incrementing 65535 wraps around to 0. If you want the counter to reset each day, now you need to store a reference date, in a side file or maybe in a comment inside AssemblyInfo.cs. For the arbitrary number, you can re-use what's in the file, or hard-code a replacement.

Time issue: Beginning and ending Daylight Savings Time make the local time occasionally jump ahead by one hour (not a problem) and fall back by one hour (always a worry). To almost-guarantee no duplicate time ranges, you can use UTC or maybe the local time except ignore Daylight Savings Time. Local time without Daylight Savings Time is a compromise used by many web servers, and even the optional automatic date-based version numbering built into VS (it overrides a.b.* with a.b.{days since Jan. 1, 2000 at 00:00}.{seconds since Jan. 1, 2000 at 00:00 divided by 2}, so it is not human readable).

Possible simplifications: •The target filename is always "AssemblyInfo.cs", so it could be specified in the program and omitted from the argument: "$(ProjectDir)Properties". •If VS executes the program with the current directory set to "D:\Users\myusername\Documents\Visual Studio 2010\Projects\solutionfolder\projectfolder\Properties", it doesn't need an argument.

•A program can read its AssemblyVersion via System.Reflection.Assembly.GetExecutingAssembly().GetName().Version .
•File:Properties displays AssemblyFileVersion.
•AssemblyFileVersion (if not specified) defaults to AssemblyVersion .

like image 164
A876 Avatar answered Oct 23 '22 03:10

A876


This can be done via MSBUILD. Here is a good description for it. You don't need a build server for this. You can call your custom MSBUILD target in the BeforeBuild Target of your project. For that open your csproj file with an editor and locate this section at the end of the file:

<!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
like image 27
Matthias Avatar answered Oct 23 '22 05:10

Matthias