Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding compilation date to the code [duplicate]

Tags:

c#

.net

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

How i can do that thing ?

Thanks

like image 209
Night Walker Avatar asked Feb 10 '10 06:02

Night Walker


2 Answers

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

like image 193
Christian Casutt Avatar answered Oct 18 '22 09:10

Christian Casutt


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.

like image 23
womp Avatar answered Oct 18 '22 09:10

womp