I have a .NET 5.0 console application. How can I create a version resource and then display that version in my application?
In the past, I have seen an AssemblyInfo.cs file. But I don't see anything like that in my new project.
I have a .NET 5.0 console application. How can I create a version resource and then display that version in my application?
In the past, I have seen an AssemblyInfo.cs file. But I don't see anything like that in my new project.
I had to solve this problem recently: how do you know which version of a tool is deployed? And how can you automate the version number, so you don't accidentally use an old version?
In the past, that information was stored as attributes in AssemblyInfo.cs. In .NET Core, those attributes are now generated by project properties at runtime.
Andrew Lock explains how the various properties like Version
, VersionPrefix
, VersionSuffix
,AssemblyVersion
FileVersion
, PackageVersion
, InformationalVersion
are used.
The most important ones are Version
, VersionPrefix
and VersionSuffix
. That's because Version
is used as the default for the other ones. Version
in turn can be calculated as VersionPrefix-VersionSuffix
if there's no explicit value.
Quoting the article:
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.
If you use System.Commandline, the version info displayed by --version
is the InformationalVersion
whose default comes from Version
.
The default VersionPrefix
is 1.0.0
and the default suffix is empty, resulting in version 1.0.0
.
*Formulas
The nice thing is that the properties can have formulas. I was able to automatically generate a date-related version number simply by adding this to a csproj
that contains no other version information
<VersionSuffix>$([System.DateTime]::UtcNow.ToString(`yyyyMMdd-HHmm`))</VersionSuffix>
Each time I build my tools now I get versions like 1.0.0-2021061501836
Overriding the properties
The project values can be overridden during build, either by specifying an explicit version suffix, or specifying an explicit value for the project properties, eg :
dotnet build --configuration Release --version-suffix preview2-final
Or
dotnet build --configuration Release /p:Version=1.2.3-preview2-final
This way, an automated release pipeline can specify a new version that overrides anything set in the project file