c# - 定义控制台应用程序版本,并从应用程序引用版本信息

标签 c# .net .net-5

我有一个 .NET 5.0 控制台应用程序。如何创建版本资源,然后在我的应用程序中显示该版本?

过去,我见过一个AssemblyInfo.cs 文件。但我在我的新项目中没有看到类似的东西。

最佳答案

我最近不得不解决这个问题:你怎么知道部署了哪个版本的工具?以及如何自动生成版本号,以免意外使用旧版本?

过去,该信息作为属性存储在 AssemblyInfo.cs 中。在 .NET Core 中,这些属性现在由项目属性在运行时生成。

Andrew Lock解释了各种属性,如 VersionVersionPrefixVersionSuffixAssemblyVersion FileVersionPackageVersion, InformationalVersion 被使用。

最重要的是VersionVersionPrefixVersionSuffix。这是因为 Version 被用作其他版本的默认值。如果没有明确的值,Version 又可以计算为 VersionPrefix-VersionSuffix

引用文章:

The Version property is the value most commonly set when building .NET Core applications. It controls the default values of all the version numbers embedded in the build output, such as PackageVersion and AssemblyVersion so it's often used as the single source of the app/library version.

如果您使用 System.Commandline--version显示的版本信息为InformationalVersion,默认来自Version

默认的 VersionPrefix1.0.0 并且默认的后缀是空的,导致版本 1.0.0

*公式

好处是属性可以有公式。只需将此添加到不包含其他版本信息的 csproj

,我就能够自动生成与日期相关的版本号
<VersionSuffix>$([System.DateTime]::UtcNow.ToString(`yyyyMMdd-HHmm`))</VersionSuffix>

每次我构建我的工具时,我都会得到类似 1.0.0-2021061501836

的版本

覆盖属性

可以在构建期间覆盖项目值,方法是指定显式版本后缀,或为项目属性指定显式值,例如:

dotnet build --configuration Release --version-suffix preview2-final

或者

dotnet build --configuration Release /p:Version=1.2.3-preview2-final

这样,自动发布管道可以指定一个新版本来覆盖项目文件中设置的任何内容

关于c# - 定义控制台应用程序版本,并从应用程序引用版本信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67988906/

相关文章:

c# 比较 DateTime start 和 DateTime stop 与 List<DateTime>

c# - 如何在 C# 中获取存储过程的返回值?

c# - Web 方法和数据库连接

.net - 从 .NET 3.5 到 PHP Web 服务的 SOAP 附件

c# - 需要帮助将 winform 迁移到 net 5

c# - 如何将子类作为参数而不是父类传递?

c# - xna 4.0 需要特殊混合

c# - 如何有效地终止 C# 中的线程?

c# - Json 响应空值

.net-core - 即使我使用 .Net Core 3.1,我也可以更新到 .Net 5 NuGet 包吗?