Determining Assembly Compile Time in VB.NET

I recently came across a function for determining the date and time a .NET assembly was compiled on. This works for EXEs and DLLs.

Apparently, the date and time that the compiled objects of an assembly are linked together is stored in the header of the final output as a count of the number of seconds that have elapsed since midnight on Jan 1, 1970. Here’s the function that reads this information from the file header and returns it as a DateTime variable:

    ''' <summary>
    ''' Returns the date and time that the specified assembly was compiled on.
    ''' </summary>
    ''' <param name="filePath">A full path to a .NET assembly.</param>
    ''' <returns>A DateTime value.</returns>
    Function RetrieveLinkerTimestamp(ByVal filePath As String) As DateTime

        Const PortableExecutableHeaderOffset As Integer = 60
        Const LinkerTimestampOffset As Integer = 8

        Dim b(2047) As Byte
        Dim s As IO.Stream = Nothing

        Try
            s = New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read)
            s.Read(b, 0, 2048)
        Finally
            If Not s Is Nothing Then s.Close()
        End Try

        Dim i As Integer = BitConverter.ToInt32(b, PortableExecutableHeaderOffset)
        Dim secondsSince1970 As Integer = BitConverter.ToInt32(b, i + LinkerTimestampOffset)
        Dim dt As New DateTime(1970, 1, 1, 0, 0, 0)

        dt = dt.AddSeconds(secondsSince1970)
        dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours)

        Return dt

    End Function

I found this function at http://www.codinghorror.com/blog/2005/04/determining-build-date-the-hard-way.html

For more info, check out http://msdn.microsoft.com/en-us/library/ms680313

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s