2011-08-30 45 views

回答

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")); 
+0

這是依賴於文件系統,雖然,它不包含編譯的日期。 –

+0

好吧,我想我找到了更好的方法 –

+0

謝謝,它看起來不錯! –

相關問題