How do I find the current time and date at compilation time in .net/C# application?

前端 未结 7 2196
孤城傲影
孤城傲影 2020-12-06 12:43

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

7条回答
  •  孤街浪徒
    2020-12-06 13:17

    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;
       }
    }
    

提交回复
热议问题