Karl Cassar
Karl Cassar

Reputation: 6473

update automatically constant value in code on each build in C# / Visual Studio

I would like to have a constant whose value would be updated automatically each time a project is built with the current date time. Is this possible?

For example, I would have a constant:

string LastBuildDate = "01/07/2014 19:20 PM"

Then, if I build 15 minutes later, this would automatically be updated to:

string LastBuildDate = "01/07/2014 19:35 PM"

It doesn't have to be specifically such a constant. I would like to be able to know when the application was last compiled and show it in the administration area of a website.

Upvotes: 2

Views: 1520

Answers (3)

Viktor Arsanov
Viktor Arsanov

Reputation: 1593

I would suggest using T4 template.

Insert the following code into your project file.

<PropertyGroup>
    <TransformOnBuild>true</TransformOnBuild>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v12.0\TextTemplating\Microsoft.TextTemplating.targets" />

And create .tt file like the following:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ output extension=".cs" #>
namespace MyNamespace
{
    public static class Constants
    {
        public static string LastBuildDate = "<#=DateTime.Now#>";
    }
}

It will create the .cs file with content like, for example

namespace MyNamespace
{
    public static class Constants
    {
        public static string LastBuildDate = "07/02/2014 10:34:46";
    }
}

Note: the path to the text template targets file may differ depending on installed visual studio version. Also, you should insert "<Import Project ..." line after the line that imports "Microsoft.CSharp.targets"; otherwise it will not work.

Upvotes: 1

Karl Cassar
Karl Cassar

Reputation: 6473

Apparently, the same functionality I was looking for can be done by setting the version in AssemblyInfo.cs to 1.0.*, as per below.

[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]

Then, I added the below code:

    public static DateTimeOffset GetLastApplicationBuildDateFromAssembly()
    {

        int gmtOfBuild = 1; //as the date/time is of the local machine where it was built, in my case +1 gmt

        var executingAssembly = Assembly.GetCallingAssembly();
        var executingAssemblyName = executingAssembly.GetName();
        var version = executingAssemblyName.Version;
        DateTimeOffset relativeDate = new DateTimeOffset(2000, 1, 1, 0, 0, 0, new TimeSpan(gmtOfBuild , 0, 0));

        int daysAfter1stJan2000 = version.Build;
        int secondsdAfterMidnight = version.MinorRevision * 2;

        var buildDate = relativeDate.AddDays(daysAfter1stJan2000).AddSeconds(secondsdAfterMidnight);
        return buildDate;

    }

Reference: Can I automatically increment the file build version when using Visual Studio?

Upvotes: 0

C5m7b4
C5m7b4

Reputation: 108

I use a plugin for Visual Studio called Build Version Increment and I set it to do Year/month day for my version so Every time I compile it updates the AssemblyInfo.cs, then I just use this code snippet to display the version in a console and usually in the header of all my apps. I have to keep multiple pieces of software installed and updated on 400 machines so its nice to easily know who has what version:

code snippet:

Version vs = new Version(Application.ProductVersion);
string _myVersion = vs.Major + "." + vs.Minor + "." + vs.Build + "." + vs.Revision; 

Hope this helps, C5

Upvotes: 1

Related Questions