ASP.NET - 在屏幕底部显示应用程序构建日期/信息

pea*_*ewg 29 versioning asp.net

我有一个asp.net Web应用程序,它在网络中的不同客户服务器上部署了许多版本.我们的一种做法是让客户在遇到问题时通过电子邮件发送截图.

在旧的asp.net 1.1天中,我们可以使用反射获取构建DLL的详细信息,并在一个微妙的位置显示有关构建日期和编号的信息.

使用.NET 2.0及更高版本,构建模型已更改,此机制不再有效.我听说过不同的构建系统,但我真的在3.5框架上寻找最简单的方法来做这个功能在框架1.1上做的事情.

  1. 每次执行构建时,都要更新构建日期/时间,并以某种方式更新构建号
  2. 能够看到构建时间戳和数字,以显示在屏幕上
  3. 尽量简单实施

Jør*_*sen 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());
Run Code Online (Sandbox Code Playgroud)


g .*_*g . 18

我们正在使用.Net 2.0并将版本信息从程序集中提取出来.也许并不理想,但我们使用描述来存储构建日期.

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

构建过程使用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>
Run Code Online (Sandbox Code Playgroud)


小智 18

我正在使用.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;

}
Run Code Online (Sandbox Code Playgroud)


CMS*_*CMS 7

您可以通过反射获得Assembly Build日期,请查看以下示例: