ASP.NET - show application build date/info at the bottom of the screen

假装没事ソ 提交于 2019-11-27 18:22:23

We are using .Net 2.0 and pull the version information out of the assembly. Perhaps not ideal, but we use the description to store the build date.

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

The build process uses asminfo nant task to generate the AssemblyInfo.cs file that contains this information.

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

I chose to just use the date of the executing assembly. The way I publish the files, this works fine.

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());

I'm using .NET 2.0 and 3.5 and it is able to set both the build number and build date. While the help panel says that if yet let .NET set it, it will use a random number for the revision, that isn't true, it actually puts date/time info that can be easily extracted, which is confirmed by the online docs: http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.assemblyversionattribute.aspx

See this blog: http://dotnetfreak.co.uk/blog/archive/2004/07/08/determining-the-build-date-of-an-assembly.aspx?CommentPosted=true#commentmessage

I want to set the build version myself but still want the automatic date/time stamp so I use something like this for the AssemblyVersion("1.0.*")

Here is a sample function to extract the build date/time

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;

}
CMS

You can get the Assembly Build date through reflection, check this examples:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!