希望标题有所描述。
我有一个用C#和.net 2.0编写的winform应用程序。我希望自动更新上次编译日期到一个变量中,以便在关于框和初始闪屏框中使用。目前我手动更新一个字符串变量。有没有办法实现这个功能?
VS2008 .net 2.0 c#
另一个技巧(可能不适用于您)是利用.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://dev59.com/83RA5IYBdhLWcg3w2xsI#804895
意思是:这里提供了一个备选答案,可能比我提供的更容易理解。请点击链接查看。echo public static class DateStamp { public readonly static System.DateTime BuildTime = System.DateTime.Parse("%date%"); } > $(ProjectDir)DateStamp.cs
public static class DateStamp
{
public readonly static System.DateTime BuildTime = System.DateTime.Parse("14/12/2009");
}
你可以像这样使用它:
Console.WriteLine("Build on " + DateStamp.BuildTime.ToShortDateString());
<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中添加,因此如果您需要支持低于该版本的版本,则可以定义自己的自定义属性类型来保存日期值。
AssemblyMetadataAttribute
的参数)-但奇怪的是,如果我先进行初始正常项目构建,然后再进行发布构建,它就可以工作。奇怪。 - Dai我使用了来自codeproject.com的这个关于框。它实际上是由Jeff Atwood在2004年编写的。它通过查看程序集文件的日期戳或从程序集版本计算来确定编译时间。也许你可以从中提取相关的代码。
这方面没有内置的宏或类似的东西。你最好的选择可能是使用自定义构建任务,例如在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' />