如何在 .net/C# 应用程序中在编译时找到当前时间和日期? [英] How do I find the current time and date at compilation time in .net/C# application?
问题描述
我想在 .net 应用程序中包含当前时间和日期,以便我可以将其包含在启动日志中,以向用户显示他们拥有的版本.是否可以在编译期间检索当前时间,或者我是否必须获得可执行文件的创建/修改时间?
I want to include the current time and date in a .net application so I can include it in the start up log to show the user what version they have. Is it possible to retrieve the current time during compilation, or would I have to get the creation/modification time of the executable?
例如
欢迎使用 ApplicationX.这是在时间日-月-年构建的.
Welcome to ApplicationX. This was built day-month-year at time.
推荐答案
如果您使用反射作为内部版本号,您可以使用它来确定何时编译了内部版本.
If you're using reflection for your build number you can use that to figure out when a build was compiled.
程序集的版本信息由以下四个值组成:
Version information for an assembly consists of the following four values:
- 主要版本
- 次要版本
- 版本号
- 修订
您可以指定所有值,也可以使用星号 (*) 接受默认版本号、修订号或两者.默认情况下,内部版本号和修订版基于 2000 年 1 月 1 日.
You can specify all the values or you can accept the default build number, revision number, or both by using an asterisk (*). Build number and revision are based off Jan 1, 2000 by default.
以下属性将设置主要和次要,但随后会增加内部版本号和修订版.
The following attribute will set Major and minor, but then increment build number and revision.
[assembly: AssemblyVersion("5.129.*")]
然后你可以使用这样的东西:
Then you can use something like this:
public static DateTime CompileTime
{
get
{
System.Version MyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
// MyVersion.Build = days after 2000-01-01
// MyVersion.Revision*2 = seconds after 0-hour (NEVER daylight saving time)
DateTime compileTime = new DateTime(2000, 1, 1).AddDays(MyVersion.Build).AddSeconds(MyVersion.Revision * 2);
return compileTime;
}
}
这篇关于如何在 .net/C# 应用程序中在编译时找到当前时间和日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!