33

I have a asp.net web application which has a number of versions deployed on different customer servers inside their networks. One practice that we have is to have clients email screenshots when they have issues.

In the old asp.net 1.1 days, we could grab details of the build DLL, using reflection, and show info about the build date and numbering on the screen in a subtle location.

With .NET 2.0 and higher, the build model changed, and this mechanism no longer works. I have heard of different build systems out there, but I'm really looking for the simplest way, on the 3.5 framework, to do what this functionality did on framework 1.1.

  1. Every time a build is performed, update the build date/time, and somehow update the build number
  2. Be able to see the build timestamp and number, to display on the screen
  3. be as simple to implement as possible
1

4 Answers 4

44

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());
2
  • I use an incrementing build number, so I cannot use any of that 'number of seconds'-purple-magic that some posts use with the version numbers. Commented Mar 24, 2009 at 13:31
  • I tried this method in my blazor webassembly, i am getting empty value in Location. Commented Jan 23, 2023 at 16:48
19

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>
2
  • That is exactly the type of solution I was looking for. Good form.
    – pearcewg
    Commented Nov 27, 2008 at 17:33
  • 1
    I had some code very similar to this, I ended up chucking it in to a custom control so you can just drag and drop it in :)
    – Rob Cooper
    Commented Nov 27, 2008 at 17:56
18

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;

}
1
  • This is very useful and doesn't require anything other than one quick change in the AssemblyInfo.cs! :-)
    – Filip
    Commented Jan 25, 2012 at 19:37
5

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

3
  • Nice find with the Coding Horror solution - I definitely do not like the default solution which relies on the assembly version following the major.minor.*.0 pattern, as I always set assembly version to major.minor.0.0 and assembly file version to major.minor.build.revision, so that I can hotfix assemblies easily. Reading the linker's own timestamp is very cunning. +1s all round! Commented Feb 14, 2013 at 0:45
  • 1
    It's better to include the relevant parts of the information in the links here in the answer.
    – Alex
    Commented Jul 6, 2020 at 9:20
  • Second link does not work. Please summarise the information from the links in a self-contained answer
    – M.M
    Commented Jan 10, 2023 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.