8

I need the date of compilation to be somehow hard coded in the code .

How i can do that thing ?

Thanks

Night Walker
  • 20,638
  • 52
  • 151
  • 228

4 Answers4

6

In most of my Projects i use a function 'RetrieveLinkerTimestamp'.

        public DateTime RetrieveLinkerTimestamp(string filePath)
    {
        const int PeHeaderOffset = 60;
        const int LinkerTimestampOffset = 8;

        byte[] b = new byte[2048];
        Stream s = Stream.Null;
        try
        {
            s = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            s.Read(b, 0, 2048);
        }
        finally
        {
            if ((s != null)) s.Close();
        }

        int i = BitConverter.ToInt32(b, PeHeaderOffset);

        int SecondsSince1970 = BitConverter.ToInt32(b, i + LinkerTimestampOffset);
        DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
        dt = dt.AddSeconds(SecondsSince1970);
        dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
        return dt;
    }

maybe this is of help?

cheers,

Christian

Christian Casutt
  • 2,334
  • 4
  • 29
  • 38
2

You could use a T4 template to generate your code wherever you need meta-information like this.

<#@ assembly name="System.Core.dll" #>
<#@ template language="C#v3.5" debug="True" hostspecific="True"  #>
<#@ output extension=".cs" #>
using System;

namespace MyNamespace
{
    public class MyClass
    {
        // <#= DateTime.Now.ToString() #>
    }
}

That template will output a class file that looks like:

using System;

namespace MyNamespace
{
    public class MyClass
    {
        // 2/9/2010 11:06:59 PM
    }
}

You might need to add a build task or a pre-build step to run the T4 compiler on every build.

Community
  • 1
  • 1
womp
  • 115,835
  • 26
  • 236
  • 269
0

Source control or Continuious integration build server? not exactly compile time and in code, but it gets a commit time and build number.

Patrick Kafka
  • 9,795
  • 3
  • 29
  • 44
0

There's probably nothing built into the language like this but you may find the following link helpful. How do I find the current time and date at compilation time in .net/C# application?

Community
  • 1
  • 1
Faisal
  • 4,054
  • 6
  • 34
  • 55