Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in DevOps and Agile by (29.3k points)

That's well and good except it means nothing to most of the users, who want to know if they have the latest build - they tend to refer to it as "last Thursday's" rather than build 1.0.8.4321.

The plan is to put the build date there instead - So "App built on 21/10/2009" for example.

I'm struggling to find a programmatic way to pull the build date out as a text string for use like this.

For the build number, I used:

Assembly.GetExecutingAssembly().GetName().Version.ToString()

after defining how those came up.

I'd like something like that for the compile date (and time, for bonus points).

Pointers here much appreciated (excuse pun if appropriate), or neater solutions.

1 Answer

0 votes
by (50.2k points)

You can get the build date using the following code:

public static DateTime GetLinkerTimestampUtc(Assembly assembly)

{

    var location = assembly.Location;

    return GetLinkerTimestampUtc(location);

}

public static DateTime GetLinkerTimestampUtc(string filePath)

{

    const int peHeaderOffset = 60;

    const int linkerTimestampOffset = 8;

    var bytes = new byte[2048];

    using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

    {

        file.Read(bytes, 0, bytes.Length);

    }

    var headerPos = BitConverter.ToInt32(bytes, peHeaderOffset);

    var secondsSince1970 = BitConverter.ToInt32(bytes, headerPos + linkerTimestampOffset);

    var dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    return dt.AddSeconds(secondsSince1970);

}

And to get the build date using

GetLinkerTimestampUtc(Assembly.GetExecutingAssembly());

Note: This method doesn't work if you compile your application with /deterministic flag 

Related questions

0 votes
1 answer
asked Jul 24, 2019 in Java by Nigam (4k points)

31k questions

32.4k answers

501 comments

693 users

Browse Categories

...