我如何找到在编译时的.NET / C#应用程序的当前日期和时间? [英] How do I find the current time and date at compilation time in .net/C# application?

查看:204
本文介绍了我如何找到在编译时的.NET / C#应用程序的当前日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包括在.NET应用程序的当前日期和时间,所以我可以将其包含在启动日志,以显示他们有什么版本的用户。是否有可能获取在编译过程中的当前时间,不然我就一定得可执行文件的创建/修改时间?

例如。


  

欢迎到ApplicationX。这个建的日 - 月 - 年的在的时间



解决方案

如果你使用反射你的版本号,你可以用它来计算出,当一个构建编译的。

程序集的版本信息由下面四个值:


  1. 主要版本

  2. 次版本

  3. 内部版本号

  4. 修改

您可以指定所有这些值,也可以使用星号(*)接受默认的内部版本号,版本号,或两者兼而有之。版本号和修订默认情况下,基于关闭2000年1月1日。

下面的属性将设置主要和次要,但随后递增版本号和修订。

  [装配:的AssemblyVersion(5.129 *)]

然后你可以使用这样的:

 公共静态的DateTime编译时
{
   得到
   {
      System.Version MyVersion = System.Reflection.Assembly.GetExecutingAssembly()的GetName()版本。;
      // MyVersion.Build = 2000-01-01天后
      // MyVersion.Revision * 2 = 0秒 - 小时(NEVER夏令时间)后
      日期时间编译时=新的日期时间(2000年,1,1).AddDays(MyVersion.Build).AddSeconds(MyVersion.Revision * 2);
      返回编译时;
   }
}

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?

E.g.

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:

  1. Major Version
  2. Minor Version
  3. Build Number
  4. Revision

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆