Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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
0 votes
1 answer
0 votes
1 answer
asked Jul 24, 2019 in Java by Nigam (4k points)
+2 votes
2 answers
asked May 23, 2019 in Python by Anvi (10.2k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...