MSBuild 输出不正确的 DateTime.UtcNow 格式

MSBuild outputting incorrect DateTime.UtcNow format

对于我在 csproj 中的版本标签,我有以下内容:

    <PropertyGroup>
        <Version>
            $([System.DateTime]::UtcNow.Year).$([System.DateTime]::UtcNow.ToString("MM")).$([System.DateTime]::UtcNow.ToString("dd")).$([System.DateTime]::UtcNow.ToString("HHmm"))
        </Version>
    </PropertyGroup>

但它的输出如下:Vysn.Voice.2020.1.9.309.nupkg 对于 nuget 包,当我从程序集获取版本信息时:Assembly.GetExecutingAssembly().GetName().Version.

但是当我这样做的时候

var assembly = Assembly.GetExecutingAssembly();
            var informationalVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

Console.WriteLine(informationalVersionAttribute.InformationalVersion);

它输出正确的 DateTime 字符串:2020.01.08.1018。我不确定在指定版本时我是否做错了什么但我希望它正确输出版本:2020.MM.dd.HHmm.

But it outputs like so: Vysn.Voice.2020.1.9.309.nupkg for nuget packages

这是设计使然的行为。

  1. 首先,Vysn.Voice被称为包标识符2020.1.9.309被命名为包版本。如 official docs 所述:

    A specific package is always referred to using its package identifier and an exact version number

    • 默认情况下,软件包版本Version匹配。因此,您可以通过自定义 *.csproj 中的 <Version/> 元素来更改包版本。请注意软件包版本不会影响软件包标识符
    • 默认情况下,包标识符与 *.csproj 文件中 <PackageId>Vysn.Voice</PackageId> 的元数据相匹配。如果 <PackageId/> 未指定或为空,它将使用 AssemblyName。有关详细信息,请参阅 Package Id
  2. 此外,请注意 <Major>.<Minor>.<Build> 中的版本号应为 Integer。当使用 NuGet 打包项目时,版本号中的 前导零 总是从 Major 删除 , Minor, 和 Build 部分。因此,您将得到 2020.1.9 而不是 2020.01.09。此行为记录在案 here.

  3. 最后,当你调用

    assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
    

    您实际上是在尝试获取不同于 VersionPackageVersionInformationalVersionInformationalVersion 可能 包含任何字符串 。这个 InformationalVersion 默认匹配 Version,可能看起来像 2020.01.09.0704(注意会有一些前导零。)