开发者

How to set "release date" constant for a project in Visual Studio?

I would like to add to my console application the information (e.g. to the "initial screen" or to the "usage screen") about when the program was last released (compiled).

C开发者_StackOverflow社区an I add this to my project?

Thanks!


Try this or down for a better method!

DateTime buildDate = 
   new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime;

To use it, for example

Console.WriteLine(buildDate.ToString("dddd, dd MMMM yyyy HH:mm:ss"));

Should output in a format like Tuesday, 30 August 2011 09:44:07

Edit: apparently that dependent on the file system but I found this page

Here is it converted to 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;
}

Credit goes to Jeff Atwood

So you should be able to use it like this

Console.WriteLine(RetrieveLinkerTimestamp().ToString("dddd, dd MMMM yyyy HH:mm:ss"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜