42

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.

juan
  • 80,295
  • 52
  • 162
  • 195

6 Answers6

59

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.

juan
  • 80,295
  • 52
  • 162
  • 195
Clyde
  • 8,017
  • 11
  • 56
  • 87
  • 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 ? – Jignesh.Raj Apr 23 '13 at 04:57
  • @Jignesh.Raj Definitely check for `if (assembly != null) { ... }` and `if (fileInfo != null) { ... }` to save yourself some grief. – Jesse Chisholm Jun 05 '18 at 00:47
  • 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. – thomasgalliker Dec 16 '19 at 23:13
15

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);
Hallgrim
  • 15,143
  • 10
  • 46
  • 54
  • Hallgrim's answer works well, also. Just make sure you modify the assembly version, not the file version (*oops*) –  Apr 20 '10 at 14:19
  • 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. – lance Mar 12 '12 at 15:34
13

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;
Markus Nigbur
  • 409
  • 2
  • 6
5

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

Community
  • 1
  • 1
Mubashar
  • 12,300
  • 11
  • 66
  • 95
3

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)
Joseph
  • 380
  • 3
  • 16
2

I don't believe the assembly holds last modified information as that is an operating system attribute. I believe the only way to get this information is through a file handle.

Josh
  • 10,352
  • 12
  • 58
  • 109