tags:

views:

126

answers:

4

Hello all

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

How i can do that thing ?

Thanks

A: 

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

sadboy
You mean to read from the code the date of my checkout ? Sounds little bit strange solution.
Night Walker
+2  A: 

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.

womp
A: 

There's probably nothing built into the language like this but you may find the following link helpful. http://stackoverflow.com/questions/971476/how-do-i-find-the-current-time-and-date-at-compilation-time-in-net-c-applicatio

Faisal
+3  A: 

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
I called it with System.Reflection.Assembly.GetExecutingAssembly().LocationAnd it worked perfect
Night Walker