如何在设计时自动插入构建日期

时间:2009-12-14 21:22:50

标签: c# .net winforms

希望标题完全具有描述性。

我有一个用C#和.net 2.0编写的winform应用程序。我想将最后一个编译日期自动更新为变量,以便在about框和初始启动框中使用。目前我有一个我手动更新的字符串变量。有没有办法做到这一点?

VS2008 .net 2.0 C#

5 个答案:

答案 0 :(得分:5)

另一个技巧(您可能无法使用)是利用.NET生成的自动构建和修订号。如果您的AssemblyInfo有:

[assembly: AssemblyVersion("1.0.*")]

最后两个数字只是一个日期/时间戳。一些代码(下面的列表)可能有所帮助:

Version v = Assembly.GetExecutingAssembly().GetName().Version;
DateTime compileDate = new DateTime((v.Build - 1) * TimeSpan.TicksPerDay + v.Revision * TimeSpan.TicksPerSecond * 2).AddYears(1999);

编辑:这是一个替代答案,可能比我说的更清楚: https://stackoverflow.com/a/804895/2258

答案 1 :(得分:3)

将此内容放入预制活动中;

echo public static class DateStamp { public readonly static System.DateTime BuildTime = System.DateTime.Parse("%date%"); }  > $(ProjectDir)DateStamp.cs

它会像这样创建一个名为DateStamp的类;

public static class DateStamp
{ 
   public readonly static System.DateTime BuildTime = System.DateTime.Parse("14/12/2009"); 
}

你就这样使用它;

Console.WriteLine("Build on " + DateStamp.BuildTime.ToShortDateString());

答案 2 :(得分:1)

我使用了codeproject.com中的this about box。它实际上是由Jeff Atwood在2004年编写的。它通过查看汇编文件上的日期戳或从汇编版本计算它来计算编译时间。也许你可以从那里提取相关的代码。

答案 3 :(得分:1)

这可能是十年太晚而无法发挥作用,但也许它会帮助其他人。

您可以将此目标添加到csproj文件中,只需将其粘贴到最后:

 <Target Name="BuildDate" BeforeTargets="CoreCompile">
    <PropertyGroup>
      <SharedAssemblyInfoFile>$(IntermediateOutputPath)CustomAssemblyInfo.cs</SharedAssemblyInfoFile>
    </PropertyGroup>
    <ItemGroup>
      <Compile Include="$(SharedAssemblyInfoFile)" />
    </ItemGroup>
    <ItemGroup>
      <AssemblyAttributes Include="AssemblyMetadata">
        <_Parameter1>AssemblyDate</_Parameter1>
        <_Parameter2>$([System.DateTime]::UtcNow.ToString("u"))</_Parameter2>
      </AssemblyAttributes>
    </ItemGroup>
    <WriteCodeFragment Language="C#" OutputFile="$(SharedAssemblyInfoFile)" AssemblyAttributes="@(AssemblyAttributes)" />
  </Target>

这将在“obj”文件夹中创建一个CustomAssemblyInfo.cs文件,该文件包含一行:[assembly: AssemblyMetadata("AssemblyDate", "DATE_OF_BUILD_HERE")] 此文件将被编译到程序集中,因此您可以在运行时使用反射访问它,例如使用以下代码:

using System;
using System.Linq;
using System.Reflection;

public class Application
{
    static DateTime BuildDate;
    public static DateTime GetBuildDate()
    {
        if (BuildDate == default(DateTime)) {
            var attr = typeof(Application).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>();
            var dateStr = attr.Single(a => a.Key == "AssemblyDate")?.Value;
            BuildDate = DateTime.Parse(dateStr);
        }
        return BuildDate;
    }
}

有一点需要注意,我在这里使用的AssemblyMetadataAttribute仅在.NET 4.5.3中添加,因此如果您需要支持之前的版本,您只需定义自己的自定义属性类型即可保存日期值。

答案 4 :(得分:0)

没有内置宏或类似的内容。您最好的选择可能是自定义构建任务,例如在tigris上,使用Time任务和RegexReplace或类似任务。做起来并不简单。我个人使用存储库版本更新AssemblyInfo - 广泛相关?

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
...
<SvnInfo LocalPath=".">
  <Output TaskParameter="Revision" PropertyName="BuildRev" />
</SvnInfo>
<FileUpdate Files="protobuf-net\Properties\AssemblyInfo.cs"
        Regex='(\[\s*assembly:\s*AssemblyVersion\(\s*"[^\.]+\.[^\.]+)\.([^\.]+)(\.)([^\.]+)("\)\s*\])'
        ReplacementText='$1.$2.$(BuildRev)$5' />