2008-11-27 30 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()); 
相关问题