将编译日期添加到代码

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

我怎么能做那件事?

谢谢

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

也许这有帮助?

干杯,

基督教

您可以使用T4模板在需要这样的元信息的任何地方生成代码。

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

您可能需要添加构建任务或预构建步骤以在每个构建上运行T4编译器。

源代码控制或Continuious集成构建服务器? 不完全编译时间和代码,但它获得提交时间和内部版本号。

这样的语言可能没有任何内置,但您可能会发现以下链接很有帮助。 如何在编译时在.net / C#应用程序中找到当前时间和日期?