If you have access to assembly image (i.e. dll or exe file itself), you
can check NT header, described in WINNT.H 
What you need is TimeDateStamp field. You will need to calculate offset
of this structure, but math is simple. Then read data at calculated
offset. Value is numbers of seconds since 1 January 1970 and is UTC
time.  

After a while I thought I'd better mockup some code :) Add error
checking and other flavor.

using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace Assembly2
{
        public class DateTimeStamper
        {
                [STAThread]
                public static void Main(string[] args)
                {
                        DateTime time =
GetExecutableDateTime(Assembly.GetExecutingAssembly().Location);
                        MessageBox.Show(time.ToLocalTime().ToString());
                }

                public static DateTime GetExecutableDateTime(string
location)
                {
                        DateTime time = new DateTime(1970, 1, 1); //
create base date
                        using (FileStream stream = new
FileStream(location, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                                using (BinaryReader reader = new
BinaryReader(stream))
                                {
                                        stream.Position = 0x3c; //
e_lfanew in IMAGE_DOS_HEADER
                                        stream.Position =
reader.ReadInt32() + 8; // goto TimeDateStamp
                                        int stamp = reader.ReadInt32();
// read stamp
                                        time = time.AddSeconds(stamp);
// adjust date
                                }
                        }
                        return time;
                }
        }
}

Sincerely, Ilya Ryzhenkov 

> -----Original Message-----
> From: Unmoderated discussion of advanced .NET topics. 
> [mailto:[EMAIL PROTECTED] On Behalf Of Raj Malli
> Sent: Tuesday, January 25, 2005 8:31 AM
> To: [email protected]
> Subject: [ADVANCED-DOTNET] Determing time when a assembly was built
> 
> Hi guys
> 
> Is there a way by which I can programmatically determine the 
> build time of an assembly? I would like to show the following 
> information in the "About"
> box of my application: Application Name, Version and Build Time.
> 
> I do not want to do the following:
>   - Use the last two parts of the assembly version to encode the time
>   - Get the last modified time from the assembly file using (say) the
>     FileInfo class
> 
> In C/C++, this is how I'd have done this:
>   #define BUILD_TIME __TIME__
>   void OnAbout() {
>      ...
>      MessageBox(BUILD_TIME);
>      ...
>   }
> I know C# supports a preprocessor. Can I do something similar 
> to the above in C#? Otherwise what other options do I have?
> 
> Thanks
> Raj
> 
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
> 
> View archives and manage your subscription(s) at 
> http://discuss.develop.com
> 

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to