Home >Backend Development >C++ >How Can I Programmatically Display My Application's Build Date?
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.
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>
To use this method, simply call GetLinkerTime() on the executing assembly:
<code class="language-c#">var linkTimeLocal = Assembly.GetExecutingAssembly().GetLinkerTime();</code>
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!