2008-11-06 81 views
2

傑夫寫了約getting a file version/datestamp回來。除非關閉/重新打開解決方案,否則Visual Studio不會增加構建,因此抓取時間戳似乎是驗證您正在使用的構建的最佳方式。如何獲取ASP.NET應用程序的構建日期?

我移植的解決方案C#

// from http://www.codinghorror.com/blog/archives/000264.html 
    protected DateTime getLinkerTimeStamp(string filepath){ 
     const int peHeaderOffset = 60; 
     const int linkerTimestampOffset = 8; 

     byte[] b = new byte[2048]; 
     Stream s = null; 

     try { 
      s = new FileStream(filepath, FileMode.Open, FileAccess.Read); 
      s.Read(b, 0, 2048); 
     } 
     finally{ 
      if (s != null){ 
       s.Close(); 
      } 
     } 

     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; 
    } 

    protected DateTime getBuildTime() 
    { 
     System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); 
     return getLinkerTimeStamp(assembly.Location); 
    } 

這似乎工作。有一個更好/更正式的方式來告訴網站何時部署?

回答

1

我認爲你最簡單的路線是在你的web.config中有一個時間戳。

在web.config中有兩種方法可以更新它。首先是使用自動構建工具,如NAnt。它可以讓你選擇修改web.config,就像你想要的一樣。這是我使用的方法。

如果您不使用自動構建工具,則可以使用另一個選項,即在Visual Studio的預生成事件中添加代碼以更新web.config。這是一篇關於Codeplex的文章,應該讓你開始。

http://www.codeproject.com/KB/dotnet/configmanager_net.aspx

+0

感謝您的指針! – Kalid 2008-11-07 00:19:18