如何手动设置程序集版本

Gra*_*zer 5 c# asp.net-core

这让我很头疼。我们过去常常将项目属性中的内容放在“[assembly: AssemblyVersion("1.0.0.0")]我完全可以接受更改”下,因此我不在乎它在哪里。我知道他们也将采用新的版本标准,也完全没问题。

大量文件都指向该project.json文件,这显然是一种浪费,因为它不再是合法文件。最近的说法是将以下内容添加到您的.csproj文件中:

<PropertyGroup>
    <VersionPrefix>1.2.3</VersionPrefix>
    <VersionSuffix>alpha</VersionSuffix>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

也完全是浪费。只是因为我似乎无法阅读它。以下总是给我1.0.0.0

PlatformServices.Default.Application.ApplicationVersion
Run Code Online (Sandbox Code Playgroud)

更不用说当我右键单击File Explorer并单击 时PropertiesDetails选项卡也总是显示1.0.0.0

那么,如何在解决方案中设置每个程序集的版本,然后在运行时读取它们?

Gra*_*zer 0

事实证明,当 .Net Core 2.0出现时,它就开始起作用了。当您右键单击该项目,然后单击“属性”时,会出现一个 UI,如下所示:

项目属性的 UI

Package VersionAssembly Version分别Assembly File Version对应文件中的VersionAssemblyVersion、设置:FileVersion.csproj

<PropertyGroup>
  <Version>1.1.0</Version>
  <AssemblyVersion>1.1.0.9</AssemblyVersion>
  <FileVersion>1.1.0.9</FileVersion>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

然后,我在解决方案的每个项目中创建了一个实用程序方法,该方法执行以下操作:

public static VersionInformationModel GetVersionInformation() {
  var Result = new VersionInformationModel {
    Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(),
    BuildDate = System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location),
    Configuration = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyConfigurationAttribute>().Configuration,
    TargetFramework = Assembly.GetExecutingAssembly().GetCustomAttribute<System.Runtime.Versioning.TargetFrameworkAttribute>().FrameworkName,
  };

  return Result;
}
Run Code Online (Sandbox Code Playgroud)

因此,在我网站的管理页面上,我可以知道每个项目的版本和其他详细信息,因为它位于我正在查看的任何服务器上。