编译时间是写到了PE头中的。
private static DateTime GetLinkerTimeStamp(string filepath)
{
const int peHeaderOffset = 60;
const int linkerTimestampOffset = 8;
byte[] b = new byte[2048];
using (Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
stream.Read(b, 0, 2048);
}
int i = BitConverter.ToInt32(b, peHeaderOffset);
int secondsSince1970 = BitConverter.ToInt32(b, i + linkerTimestampOffset);
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
dt = dt.AddSeconds(secondsSince1970);
dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
return dt;
}
/// <summary>
/// 获得当前可执行程序的构建时间
/// </summary>
/// <returns></returns>
public static DateTime GetBuildTime()
{
Assembly assembly = Assembly.GetExecutingAssembly();
return GetLinkerTimeStamp(assembly.Location);
}
参考:
1、http://stackoverflow.com/questions/267421/how-to-get-a-build-date-for-an-asp-net-application
2、http://www.cnblogs.com/skyivben/archive/2010/12/18/1910180.html
如鹏网.Net培训班正在报名,有网络的地方就可以参加如鹏网的学习,学完就能高薪就业,点击此处了解
三年前只要懂“三层架构”就可以说“精通分层架构”;现在则需要懂IOC(AutoFac等)、CodeFirst、lambda、DTO等才值钱;
三年前只要会SQLServer就可以说自己“精通数据库开发”;现在则需还需要掌握MySQL等开源数据库才能说是“.Net开源”时代的程序员;
三年前只要会进行用户上传内容的安全性处理即可;现在则需要熟悉云存储、CDN等才能在云计算时代游刃有余;
三年前只要掌握Lucene.Net就会说自己“熟悉站内搜索引擎开发”;现在大家都用ElasticSearch了,你还用Lucene.Net就太老土了;
三年前发邮件还是用SmtpClient;现在做大型网站发邮件必须用云邮件引擎;
三年前缓存就是Context.Cache;现在则是Redis、Memcached的天下;
如鹏网再次引领.Net社区技术潮流!点击此处了解如鹏网.Net最新课程