Home >Backend Development >C++ >How Can I Programmatically Display My Application's Build Date?

How Can I Programmatically Display My Application's Build Date?

Barbara Streisand
Barbara StreisandOriginal
2025-01-22 01:16:09997browse

How Can I Programmatically Display My Application's Build Date?

Show Build Date: Complete Guide

Determining the build date of your application is critical for users to determine whether they have the latest version. While displaying the build number is common, it usually provides little information. This article explores ways to programmatically extract the build date and display it in a user-friendly format.

Get build date

Unlike the build number, there is no direct way to retrieve the build date from assembly metadata. However, a reliable method is to extract the linker timestamp embedded in the PE header of the executable. The following C# code demonstrates this technique:

<code class="language-c#">public static DateTime GetLinkerTime(this Assembly assembly, TimeZoneInfo target = null)
{
    var filePath = assembly.Location;
    const int c_PeHeaderOffset = 60;
    const int c_LinkerTimestampOffset = 8;

    var buffer = new byte[2048];

    using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        stream.Read(buffer, 0, 2048);

    var offset = BitConverter.ToInt32(buffer, c_PeHeaderOffset);
    var secondsSince1970 = BitConverter.ToInt32(buffer, offset + c_LinkerTimestampOffset);
    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    var linkTimeUtc = epoch.AddSeconds(secondsSince1970);

    var tz = target ?? TimeZoneInfo.Local;
    var localTime = TimeZoneInfo.ConvertTimeFromUtc(linkTimeUtc, tz);

    return localTime;
}</code>

Usage examples

To use this method, simply call GetLinkerTime() on the executing assembly:

<code class="language-c#">var linkTimeLocal = Assembly.GetExecutingAssembly().GetLinkerTime();</code>

Instructions about .NET Core

Please note that this method will not work properly after .NET Core 1.1. The extracted linker timestamp will provide a random year in the range 1900-2020.

The above is the detailed content of How Can I Programmatically Display My Application's Build Date?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn