views:

959

answers:

5

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

Is it possible to get it though reflection?

I get the version like this, I'm looking for something similar:

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

ie: 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.

A: 

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
+3  A: 

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
+4  A: 

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.

Clyde
+1  A: 

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

Hallgrim's answer works well, also. Just make sure you modify the assembly version, not the file version (oops)

froodley