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

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

问题描述

目前,我有一个应用程序显示在其标题窗口中的版本号。这当然很好,除了它意味着什么给大多数用户,谁想要知道他们是否有最新版本的 - 他们往往把它称为上周四的,而不是建立1.0.8.4321

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.

我们的计划是把构建日期有而不是 - 所以应用程序建立在21/10/2009,例如

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).

指针这里多AP preciated(双关语的借口如果合适的话),或者更简洁的解决方案...

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.

最可靠的方法真可谓是检索从的 PE头嵌入可执行文件 - 一些C#code(乔·斯皮维),用于从评论到杰夫的文章:

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

用法示例:

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

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

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