C# 如何将应用程序的构建日期放在应用程序中的某个位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/613700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:17:29  来源:igfitidea点击:

How to put the build date of application somewhere in the application?

c#

提问by Nikhil

I would like to put the date the application was built somewhere in the application. Say the about box. Any ideas how this can be done? I need to do this for C# but I am also looking for a general idea, so you can answer this for any specific language other than C#.

我想在应用程序的某个地方放置应用程序的构建日期。说一下关于框。任何想法如何做到这一点?我需要为 C# 执行此操作,但我也在寻找一个总体思路,因此您可以针对 C# 以外的任何特定语言回答此问题。

采纳答案by BadCanyon

Typically we just go with the executable's last modify date. This will be set when the exe is built and usually never changes (short of someone actually editing the file). When the file is installed, copied, moved, etc, Windows doesn't change that value.

通常我们只使用可执行文件的最后修改日期。这将在构建 exe 时设置,并且通常不会更改(除非有人实际编辑文件)。安装、复制、移动文件等时,Windows 不会更改该值。

DateTime buildDate = 
   new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime;

We use this technique for the about dialogs in our C# and C++ apps.

我们将这种技术用于 C# 和 C++ 应用程序中的关于对话框。

回答by KristoferA

The third number of the assembly version is a julian date with 0=1 Jan 2000 if you're using [assembly: AssemblyVersion("1.0.*")]

如果您使用 [assembly: AssemblyVersion("1.0.*")],则程序集版本的第三个数字是 0=1 Jan 2000 的儒略日期

e.g.

例如

DateTime buildDate = new DateTime(2000,1,1).AddDays(
  System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Build
 );

回答by Matthew Farwell

There are usually keywords in your source code control system for this sort of thing.

在您的源代码控制系统中通常有用于此类事情的关键字。

Otherwise, look at including the date and time in the version number, or simply creating a small source code file which contains the date and time, and gets included in the build

否则,查看在版本号中包含日期和时间,或者简单地创建一个包含日期和时间的小源代码文件,并包含在构建中

回答by MarkJ

You shouldbe using version control - Subversionis free. Then you could include a number from the version control system that would unambiguously identify the source code used to build the app. A date won't do that. There are other advantages too.

应该使用版本控制 - Subversion是免费的。然后,您可以包含版本控制系统中的一个数字,该数字将明确标识用于构建应用程序的源代码。约会不会那样做。还有其他优点。

  • Full history of all changes to the project.
  • Work seamlessly with other developers on the same project.
  • 项目所有更改的完整历史记录。
  • 与同一项目的其他开发人员无缝合作。

EDIT: Nikhil is already doing all this. But for some incomprehensible reasonhe has been told to include the date as well. I'm going to leave this answer here anyway, for future readers of this question.

编辑:Nikhil 已经在做这一切。但出于某种难以理解的原因,他被告知也要包括日期。无论如何,我将把这个答案留在这里,供这个问题的未来读者使用。