将编译日期添加到代码中

时间:2010-02-10 06:51:30

标签: c# .net

我需要编译日期以某种方式在代码中进行硬编码。

我怎么能做那件事?

由于

4 个答案:

答案 0 :(得分:5)

在我的大多数项目中,我使用了一个函数'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;
    }

也许这有帮助?

欢呼,

基督教

答案 1 :(得分:2)

您可以use a T4 template生成代码,只要您需要这样的元信息。

<#@ 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() #>
    }
}

该模板将输出一个类似于:

的类文件
using System;

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

您可以need to add a build task or a pre-build step在每次构建时运行T4编译器。

答案 2 :(得分:0)

持续集成构建服务器的源代码管理?不完全编译时间和代码,但它获得提交时间和内部版本号。

答案 3 :(得分:0)

这样的语言可能没有内置任何内容,但您可能会发现以下链接很有帮助。 How do I find the current time and date at compilation time in .net/C# application?