2008-11-27 29 views
23

我有一個asp.net web應用程序,它的許多版本都部署在網絡中的不同客戶服務器上。我們的一個做法是讓客戶在遇到問題時通過電子郵件發送截圖。ASP.NET - 在屏幕底部顯示應用程序的生成日期/信息

在舊的asp.net 1.1天中,我們可以使用反射來獲取構建DLL的詳細信息,並在屏幕上以微妙的位置顯示有關構建日期和編號的信息。

在.NET 2.0及更高版本中,構建模型已更改,並且此機制不再有效。我聽說過那裏有不同的構建系統,但我真的正在尋找最簡單的方法,在3.5框架中執行此功能在框架1.1上所做的工作。

  1. 進行構建時,都會更新生成日期/時間,並以某種方式更新版本號
  2. 能夠看到構建時間戳和數量,以顯示在屏幕上
  3. 一樣簡單實現儘可能
+0

http://stackoverflow.com/questions/1168279/asp-net-version-build-number/1168329#1168329 – Saber 2011-11-01 16:06:11

回答

17

我們使用的是.NET 2.0和拉版本信息進行組裝的。也許並不理想,但我們使用描述來存儲構建日期。

Assembly assembly = Assembly.GetExecutingAssembly(); 
string version = assembly.GetName().Version.ToString(); 
string buildDate = ((AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
    assembly, typeof(AssemblyDescriptionAttribute))).Description; 

構建過程使用asminfo nant任務來生成包含此信息的AssemblyInfo.cs文件。

<asminfo output="Properties\AssemblyInfo.cs" language="CSharp"> 
     <imports> 
      <import namespace="System" /> 
      <import namespace="System.Reflection" /> 
      <import namespace="System.Runtime.CompilerServices" /> 
      <import namespace="System.Runtime.InteropServices" /> 
     </imports> 
     <attributes> 
      <attribute type="AssemblyVersionAttribute" value="${assembly.version}" /> 
      <attribute type="AssemblyInformationalVersionAttribute" value="${assembly.version}" /> 
      <attribute type="AssemblyDescriptionAttribute" value="${datetime::now()}" /> 
      ... 
     </attributes> 
    </asminfo> 
+0

這正是我所尋找的解決方案的類型。好形式。 – pearcewg 2008-11-27 17:33:32

+1

我有一些代碼非常類似於這個,我最終將它夾入到一個自定義控件中,以便您可以將其拖放到:) – 2008-11-27 17:56:02

7

你可以通過反射大會創建日期,檢查這方面的例子:

+1

「確定生成日期艱難地」爲我工作。 – zsong 2011-08-24 16:12:04

+0

不錯的發現與編碼恐怖解決方案 - 我絕對不喜歡默認的解決方案,它依賴於major.minor。*之後的程序集版本。0模式,因爲我總是將彙編版本設置爲major.minor.0.0,並將彙編文件版本設置爲major.minor.build.revision,以便我可以輕鬆地修復程序集。閱讀鏈接器自己的時間戳非常狡猾。全方位+1! – 2013-02-14 00:45:39

16

我使用.NET 2.0和3.5,它能夠設置內部版本號和生成日期。儘管幫助小組表示,如果讓.NET設置它,它將使用隨機數進行修訂,但事實並非如此,它實際上提供了可以輕鬆提取的日期/時間信息,這可以通過在線文檔進行確認: http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.assemblyversionattribute.aspx

看到這個博客: http://dotnetfreak.co.uk/blog/archive/2004/07/08/determining-the-build-date-of-an-assembly.aspx?CommentPosted=true#commentmessage

我想設置內部版本自己,但還是希望自動日期/時間戳記,所以我使用這樣的 的AssemblyVersion(「1.0 *。」 )

這裏是一個示例函數來提取構建日期/時間

private System.DateTime BuildDate() 
{ 

//This ONLY works if the assembly was built using VS.NET and the assembly version attribute is set to something like the below. The asterisk (*) is the important part, as if present, VS.NET generates both the build and revision numbers automatically. 
//<Assembly: AssemblyVersion("1.0.*")> 
//Note for app the version is set by opening the 'My Project' file and clicking on the 'assembly information' button. 
//An alternative method is to simply read the last time the file was written, using something similar to: 
//Return System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly.Location) 

//Build dates start from 01/01/2000 

System.DateTime result = DateTime.Parse("1/1/2000"); 

//Retrieve the version information from the assembly from which this code is being executed 

System.Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; 

//Add the number of days (build) 

result = result.AddDays(version.Build); 

//Add the number of seconds since midnight (revision) multiplied by 2 

result = result.AddSeconds(version.Revision * 2); 

//If we're currently in daylight saving time add an extra hour 

if (TimeZone.IsDaylightSavingTime(System.DateTime.Now, TimeZone.CurrentTimeZone.GetDaylightChanges(System.DateTime.Now.Year))) 
{ 
    result = result.AddHours(1); 
} 

return result; 

} 
33

我選擇只使用執行程序集的日期。 我發佈文件的方式,這工作正常。

lblVersion.Text = String.Format("Version: {0}<br>Dated: {1}", 
    System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), 
    System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location).ToShortDateString()); 
相關問題