【问题标题】:How do I get the last write or build time of my app? [duplicate]如何获得我的应用程序的最后一次写入或构建时间? [复制]
【发布时间】:2012-07-17 17:40:10
【问题描述】:

可能重复:
How can I display the Build number and/or DateTime of last build in my app?

我想将构建应用程序的最后一个日期时间附加到标题栏。但是这段代码:

DateTime lastWriteTime = File.GetLastWriteTime(Assembly.GetExecutingAssembly().GetName().ToString());
this.Text = String.Format("Platypi R Us; Built on {0}", lastWriteTime);

...400 多年前的声明(1600 年);我觉得不太可能。

【问题讨论】:

  • 你不是刚问了这个问题吗? stackoverflow.com/questions/11532705/…
  • @zeroef:不,但类似;我放弃了包含版本信息,因为那只是“1.0.0.0”之类的东西,并且内部版本号不可用。所以我现在正在确定一个“最后构建时间”值,它正在工作。

标签: c# .net winforms file datetime


【解决方案1】:

问题是您调用GetLastWriteTime 时使用了不是文件名的名称。打印出Assembly.GetExecutingAssembly().GetName().ToString(),你就会明白我的意思了。

documentation for File.GetLastWriteTime 明确指出:

如果路径参数中描述的文件不存在,则此方法返回 1601 年 1 月 1 日午夜 12:00,协调世界时 (UTC),调整为本地时间。

因此,根据 Clay 的建议使用 Application.ExecutablePath,或者对于特定程序集(或避免 WinForms 依赖),您可以使用 Application.ManifestModule 并获取 FullyQualifiedName,如下所示:

using System;
using System.Reflection;

class Test
{
    static void Main()
    {
        string file = typeof(Test).Assembly
                                  .ManifestModule
                                  .FullyQualifiedName;
        Console.WriteLine(file);
        DateTime lastWriteTime = File.GetLastWriteTime(file);
        Console.WriteLine(lastWriteTime);
    }
}

当然,这只获取包含程序集清单的模块的最后写入时间 - 可能有多模块程序集。不过这种情况很少见,我猜这对你没问题。

很遗憾 Assembly 本身没有嵌入构建时间的概念,但这就是生活 :(

【讨论】:

  • 有趣的是文档弄错了日期,这并不重要(它显示的是 1600,而不是 1601)。
【解决方案2】:

这行得通:

FileInfo fileInfo = new FileInfo(Application.ExecutablePath);
DateTime lastWriteTime = fileInfo.LastWriteTime;
this.Text = String.Format("Platypi R Us; Built on {0}", lastWriteTime); 

【讨论】:

    【解决方案3】:

    'GetName()' 返回程序集的显示名称。你要Assembly.Location

    另外,我不确定这是否可行。如果将文件复制到其他位置,则文件时间与实际的上次构建时间不同步。也许每次发布新版本时手动更新它?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-26
      • 2011-01-21
      • 2012-06-24
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      相关资源
      最近更新 更多