Getting the date of a .NET assembly

C#.Net 3.5

C# Problem Overview


How can I retrieve the Created date from the current .NET assembly?

I'd like to add some realy simple functionality where my app stops working one week after the build date of the main assembly. I already wrote the code that kills my app after a given date. I just need to programmatically retrieve the creation date from the assembly.

C# Solutions


Solution 1 - C#

The following is 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);
    }
}

Solution 2 - C#

I don't think the assembly itself contains it's creation date. I suspect the closest you can get is the creation date of the assembly file itself:

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

should do the trick.

EDIT:

I think Jeff Atwood's solution, written up by "grenade" in this thread, is probably the better way to go now.

Solution 3 - C#

What's wrong with:

System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location);

Solution 4 - C#

Maybe this post on coding horror may help

Solution 5 - C#

This should work:

var entryAssembly = Assembly.GetEntryAssembly();
var fileInfo = new FileInfo(entryAssembly.Location);
var buildDate = fileInfo.LastWriteTime;

Solution 6 - C#

The best way to do this would be with a custom attribute that you set on the PreBuild of your assembly.

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

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

Solution 7 - C#

If you're writing an application for a mobile device using the compact framwork, Assembly.Location is not available.

Here, I found an alternative:

     System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDenaliHardtailView Question on Stackoverflow
Solution 1 - C#grenadeView Answer on Stackoverflow
Solution 2 - C#Rob LevineView Answer on Stackoverflow
Solution 3 - C#WimView Answer on Stackoverflow
Solution 4 - C#jmlumpkinView Answer on Stackoverflow
Solution 5 - C#Jake PearsonView Answer on Stackoverflow
Solution 6 - C#Paulo SantosView Answer on Stackoverflow
Solution 7 - C#realbartView Answer on Stackoverflow