我有一个asp.net web应用程序,它在其网络中的不同客户服务器上部署了许多版本。我们的一个做法是让客户在遇到问题时用电子邮件截图。
在旧的asp.net 1.1天中,我们可以使用反射获取构建DLL的细节,并在一个微妙的位置显示有关构建日期和屏幕编号的信息。
在使用.NET 2.0和更高版本时,构建模型发生了变化,这种机制不再起作用。我听说过不同的构建系统,但是我真的在寻找最简单的方法,在3.5框架上完成这个功能在Framework1.1上所做的工作。
发布于 2008-11-27 17:16:56
我们正在使用.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>发布于 2009-03-24 13:30:38
我选择只使用执行程序集的日期。就我发布文件的方式来说,这很好。
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());发布于 2008-12-19 18:31:34
我使用的是.NET 2.0和3.5,它能够同时设置版本号和构建日期。帮助面板说,如果还让.NET设置它,它将使用一个随机数进行修订,这不是真的,它实际上会将可以轻松提取的日期/时间信息,这是由在线文档:http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.assemblyversionattribute.aspx确认的。
我想自己设置构建版本,但仍然需要自动日期/时间戳,所以我在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;
}https://stackoverflow.com/questions/324245
复制相似问题