Getting .NET build date

How can I get the created date from the current .NET assembly?

I would like to add some really simple functions when my application stops working a week after the build date of the main assembly. I already wrote code that kills my application after a certain date. I just need to programmatically get the creation date from the assembly.

+45
Jan 12 '10 at 16:18
source share
7 answers

I don’t think the assembly itself contains the creation date. I suspect that closest you can get the creation date of the assembly file itself:

File.GetCreationTime(Assembly.GetExecutingAssembly().Location) 

gotta do the trick.

EDIT:

I think the Jeff Atwood solution, written by a β€œgrenade” in this thread, is probably the best way to go now.

+41
Jan 12 '10 at 16:22
source share

Based on: https://blog.codinghorror.com/determining-build-date-the-hard-way/

 public static class ApplicationInformation { /// <summary> /// Gets the executing assembly. /// </summary> /// <value>The executing assembly.</value> public static System.Reflection.Assembly ExecutingAssembly { get { return executingAssembly ?? (executingAssembly = System.Reflection.Assembly.GetExecutingAssembly()); } } private static System.Reflection.Assembly executingAssembly; /// <summary> /// Gets the executing assembly version. /// </summary> /// <value>The executing assembly version.</value> public static System.Version ExecutingAssemblyVersion { get { return executingAssemblyVersion ?? (executingAssemblyVersion = ExecutingAssembly.GetName().Version); } } private static System.Version executingAssemblyVersion; /// <summary> /// Gets the compile date of the currently executing assembly. /// </summary> /// <value>The compile date.</value> public static System.DateTime CompileDate { get { if (!compileDate.HasValue) compileDate = RetrieveLinkerTimestamp(ExecutingAssembly.Location); return compileDate ?? new System.DateTime(); } } private static System.DateTime? compileDate; /// <summary> /// Retrieves the linker timestamp. /// </summary> /// <param name="filePath">The file path.</param> /// <returns></returns> /// <remarks>http://www.codinghorror.com/blog/2005/04/determining-build-date-the-hard-way.html</remarks> private static System.DateTime RetrieveLinkerTimestamp(string filePath) { const int peHeaderOffset = 60; const int linkerTimestampOffset = 8; var b = new byte[2048]; System.IO.FileStream 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(); } var dt = new System.DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(System.BitConverter.ToInt32(b, System.BitConverter.ToInt32(b, peHeaderOffset) + linkerTimestampOffset)); return dt.AddHours(System.TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours); } } 
+56
03 Sep '10 at 9:40
source share

What happened with:

 System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location); 
+22
Jan 12 '10 at 16:22
source share

Maybe this post on coding horror can help

+9
Jan 12 '10 at 16:23
source share

This should work:

 var entryAssembly = Assembly.GetEntryAssembly(); var fileInfo = new FileInfo(entryAssembly.Location); var buildDate = fileInfo.LastWriteTime; 
+4
Jan 12 '10 at 16:22
source share

The best way to do this is to create a custom attribute that you set on the PreBuild your assembly.

And then use standard reflection to get the attribute you created.

But out of curiosity, why kill the app after the BUILD date?

+2
Jan 12 '10 at 16:23
source share

If you are writing an application for a mobile device using a compact wireframe assembly, Assembly.Location is not available.

Here , I found an alternative:

  System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) 
+1
Jan 25 '13 at 12:14
source share



All Articles