显示构建日期 [英] Displaying the build date

查看:161
本文介绍了显示构建日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个应用程序在其标题窗口中显示版本号。这很好,除了对大多数用户来说没有意义,他们想知道他们是否有最新的版本 - 他们倾向于将其称为上周四的,而不是构建1.0.8.4321。



计划是将生成日期放在那里 - 例如App build on 21/10/2009。





对于内部版本号,我使用了:

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


我想要类似的编译日期(和时间,



指针在这里非常感谢(如果适当,借口双关语),或者更简洁的解决方案...

解决方案

Jeff Atwood在确定构建日期难的方式



最可靠的方法是从< a href =https://web.archive.org/web/20120106051754/http://support.microsoft.com/kb/65122 =nofollow noreferrer> PE标题嵌入可执行文件 - 一些C#代码(通过Joe Spivey)从评论到Jeff的文章:

  public static DateTime GetLinkerTime组件,TimeZoneInfo target = null)
{
var filePath = assembly.Location;
const int c_PeHeaderOffset = 60;
const int c_LinkerTimestampOffset = 8;

var buffer = new byte [2048];

使用(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,0,0,0,DateTimeKind.Utc);

var linkTimeUtc = epoch.AddSeconds(secondsSince1970);

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

return localTime;
}

使用示例:

  var linkTimeLocal = Assembly.GetExecutingAssembly()。GetLinkerTime(); 


I currently have an app displaying the build number in its title window. 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...

解决方案

Jeff Atwood had a few things to say about this issue in Determining Build Date the hard way.

The most reliable method turns out to be retrieving the linker timestamp from the PE header embedded in the executable file -- some C# code (by Joe Spivey) for that from the comments to Jeff's article:

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;
}

Usage example:

var linkTimeLocal = Assembly.GetExecutingAssembly().GetLinkerTime();

这篇关于显示构建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆