我有一个 .NET 5.0 控制台应用程序。如何创建版本资源,然后在我的应用程序中显示该版本?
过去,我见过一个AssemblyInfo.cs 文件。但我在我的新项目中没有看到类似的东西。
最佳答案
我最近不得不解决这个问题:你怎么知道部署了哪个版本的工具?以及如何自动生成版本号,以免意外使用旧版本?
过去,该信息作为属性存储在 AssemblyInfo.cs 中。在 .NET Core 中,这些属性现在由项目属性在运行时生成。
Andrew Lock解释了各种属性,如 Version
、VersionPrefix
、VersionSuffix
、AssemblyVersion
FileVersion
、 PackageVersion
, InformationalVersion
被使用。
最重要的是Version
、VersionPrefix
和VersionSuffix
。这是因为 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 asPackageVersion
andAssemblyVersion
so it's often used as the single source of the app/library version.
如果您使用 System.Commandline ,--version
显示的版本信息为InformationalVersion
,默认来自Version
。
默认的 VersionPrefix
是 1.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/