Soo Wei Tan Soo Wei Tan - 1 month ago 8
C# Question

Equivalent of __DATE__, __TIME__ macros in C#

Is there an equivalent of

__DATE__
and
__TIME__
in C#?

Basically what I'm trying to do is place some build timestamping in a C# application.

One possibility I saw on Microsoft's website was to do the following:

Assembly assem = Assembly.GetExecutingAssembly();
Version vers = assem.GetName().Version;
DateTime buildDate = new DateTime(2000, 1, 1).AddDays(vers.Build).AddSeconds(vers.Revision * 2);
Console.WriteLine(vers.ToString());
Console.WriteLine(buildDate.ToString());


However, this only works if your version in AssemblyInfo.cs is "1.0..", which ours won't be.

Answer

I recommend customising your build script to write the date time into the file. The MSBuild Community Tasks project has a Time task that can be used for exactly this.