How can I get the assembly last modified date?

23,168

Solution 1

I'll second pYrania's answer:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(assembly.Location);
DateTime lastModified = fileInfo.LastWriteTime;

But add this:

You mention you don't want to access the file system since it's in your master page and you don't want to make that extra file system hit for every page. So don't, just access it once in the Application load event and then store it as an application-level variable.

Solution 2

If you default the Revision and Build Numbers in AssemblyInfo:

[assembly: AssemblyVersion("1.0.*")]

You can get the an approximate build date with:

Version version = typeof(MyType).Assembly.GetName().Version;
DateTime date = new DateTime(2000, 1, 1)
    .AddDays(version.Build)
    .AddSeconds(version.Revision * 2);

Solution 3

How about this?

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(assembly.Location);
DateTime lastModified = fileInfo.LastWriteTime;

Solution 4

Some people think that Assembly doesn't holds build date but you know what they are wrong, you can be retrieve the linker timestamp from the PE header embedded in the executable file, like following may work (i havn't tested the code myself)

private DateTime RetrieveLinkerTimestamp()
{
    string filePath = System.Reflection.Assembly.GetCallingAssembly().Location;
    const int c_PeHeaderOffset = 60;
    const int c_LinkerTimestampOffset = 8;
    byte[] b = new byte[2048];
    System.IO.Stream s = null;

    try
    {
        s = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        s.Read(b, 0, 2048);
    }
    finally
    {
        if (s != null)
        {
            s.Close();
        }
    }

    int i = System.BitConverter.ToInt32(b, c_PeHeaderOffset);
    int secondsSince1970 = System.BitConverter.ToInt32(b, i + c_LinkerTimestampOffset);
    DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
    dt = dt.AddSeconds(secondsSince1970);
    dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
    return dt;
}

or if assembly is your's own better you use following approach simple and easy

Add below to pre-build event command line:

echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"

Add this file as resource, now you have 'BuildDate' string in your resources.

I have taken both answers from this question

Solution 5

The RetrieveLinkerTimestamp solution will not work after the year 2038 due to the use of int32 from 1970. I suggest using the following (although this probably has its limitations too):

IO.File.GetLastWriteTime(Reflection.Assembly.GetExecutingAssembly().Location)
Share:
23,168
juan
Author by

juan

Geek (not nerd), who programs stuff. Twitter: @juanformoso https://keybase.io/juan Here are my credentials in the network:

Updated on February 05, 2021

Comments

  • juan
    juan over 3 years

    I want to render (for internal debugging/info) the last modified date of an assembly, so I'll know when a certain website was deployed.

    Is it possible to get it through reflection?

    I get the version like this:

    Assembly.GetExecutingAssembly().GetName().Version.ToString();
    

    I'm looking for something similar -- I don't want to open the physical file, get its properties, or something like that, as I'll be rendering it in the master page, and don't want that kind of overhead.

  • Admin
    Admin about 14 years
    Hallgrim's answer works well, also. Just make sure you modify the assembly version, not the file version (oops)
  • lance
    lance about 12 years
    Careful if you're using this in a SharePoint assembly that hosts a Web Part, as SP will show (to the user) an error for Web Parts which were added to a page from an assembly whose version number has since changed. Otherwise this is a great answer.
  • Jignesh.Raj
    Jignesh.Raj about 11 years
    Hi, i tried with this code but i am getting error of location.in my case location is coming null...so, what i have to do ?
  • Michael Freidgeim
    Michael Freidgeim about 7 years
    The answer Getting the date of a .NET assembly has a helper class for it
  • Jesse Chisholm
    Jesse Chisholm almost 6 years
    @Jignesh.Raj Definitely check for if (assembly != null) { ... } and if (fileInfo != null) { ... } to save yourself some grief.
  • thomasgalliker
    thomasgalliker over 4 years
    This works perfectly as long as you don't use it on Xamarin Android in RELEASE mode. There it always returns January 1, 1601 as date.
  • Eraniichan
    Eraniichan over 3 years
    Thanks for this! this was the answer that I was looking for as I can get the epoch of the following assembly!