我想在我的控制台应用程序中添加有关程序上次发布(编译)的信息(例如“初始屏幕”或“使用屏幕”)。
我可以将它添加到我的项目中吗?
谢谢!
答案 0 :(得分:2)
尝试此或向下搜索更好的方法!
DateTime buildDate =
new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime;
使用它,例如
Console.WriteLine(buildDate.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
应以Tuesday, 30 August 2011 09:44:07
编辑:显然依赖于文件系统,但我found this page
这是转换为C#
private DateTime RetrieveLinkerTimestamp()
{
string filePath = System.Reflection.Assembly.GetCallingAssembly().Location;
const int c_PeHeaderOffset = 60;
const int c_LinkerTimestampOffset = 8;
byte[] b = new byte[2048];
System.IO.Stream s = null;
try
{
s = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
s.Read(b, 0, 2048);
}
finally
{
if (s != null)
{
s.Close();
}
}
int i = System.BitConverter.ToInt32(b, c_PeHeaderOffset);
int secondsSince1970 = System.BitConverter.ToInt32(b, i + c_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;
}
归功于杰夫阿特伍德
所以你应该能够像这样使用它
Console.WriteLine(RetrieveLinkerTimestamp().ToString("dddd, dd MMMM yyyy HH:mm:ss"));