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

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

问题描述

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



计划是将生成日期放在那里 - 所以App建立在21/10/2009,例如。



m努力找到一个程序化的方式来将构建日期作为文本字符串使用,如下所示。



对于构建号,我使用:

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

在定义如何上传之后。



我希望这样的编译日期(和时间,对于奖励积分)。



这里的指针非常赞赏(适当的是借口双关),或者更加清晰的解决方案...

解决方案

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



最可靠的方法证明是检索链接器时间戳从 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];

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(); 


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天全站免登陆