C# 如何手动设置程序集版本

C# 如何手动设置程序集版本,c#,asp.net-core,C#,Asp.net Core,这个让我头疼。我们曾经将项目属性中的内容放在[assembly:AssemblyVersion(“1.0.0.0”)]下,我完全可以更改,所以我不在乎它在哪里。我知道他们也将要为版本制定一个新标准,而且也完全可以 大量文档都指向project.json文件,这显然是一种浪费,因为它不再是合法文件。最近,请在.csproj文件中添加以下内容: <PropertyGroup> <VersionPrefix>1.2.3</VersionPrefix>

这个让我头疼。我们曾经将项目属性中的内容放在
[assembly:AssemblyVersion(“1.0.0.0”)]
下,我完全可以更改,所以我不在乎它在哪里。我知道他们也将要为版本制定一个新标准,而且也完全可以

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

<PropertyGroup>
    <VersionPrefix>1.2.3</VersionPrefix>
    <VersionSuffix>alpha</VersionSuffix>
</PropertyGroup>
更不用说我在
文件资源管理器中右键单击
并单击
属性
,然后
详细信息
选项卡也总是显示
1.0.0

PlatformServices.Default.Application.ApplicationVersion

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

方法是在项目文件中设置以下任一值:

<PropertyGroup>
    <Version>1.3.5.7</Version>
    <FileVersion>2.4.6.8</FileVersion>
</PropertyGroup>

事实证明,当.NETCore2.0推出时,它就开始工作了。当您右键单击项目然后单击属性时,会出现一个界面,如下所示:

程序包版本
程序集版本
程序集文件版本
分别对应于
.csproj
文件中的
版本
程序集版本
文件版本
设置:

<PropertyGroup>
  <Version>1.1.0</Version>
  <AssemblyVersion>1.1.0.9</AssemblyVersion>
  <FileVersion>1.1.0.9</FileVersion>
</PropertyGroup>

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

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;
}
publicstaticversioninformationmodel GetVersionInformation(){
var结果=新版本信息模型{
Version=Assembly.GetExecutionGassembly().GetName().Version.ToString(),
BuildDate=System.IO.File.GetLastWriteTime(Assembly.getExecutionGassembly().Location),
配置=Assembly.GetExecutionGassembly().GetCustomAttribute().Configuration,
TargetFramework=Assembly.GetExecutionGassembly().GetCustomAttribute().FrameworkName,
};
返回结果;
}

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

除非我遗漏了什么,否则这是不起作用的。我把
作为第二个
放在
TargetFramework
之后
netcoreapp1.1
中的一个
fileVersion
informationalVersion
作为
1.0.0
返回,文件资源管理器|属性|详细信息也显示为
1.0.0
。我使用的是错误的版本还是.Net内核还是什么?啊!您不需要新的属性组,只需将这些值与目标框架一起放入。将版本移动到第一个
PropertyGroup
不会改变任何内容。我明白你的意思,不要更改自动生成的文件,但我甚至在任何地方都看不到AssemblyInfo.cs文件。即使在重建之后。我一定遗漏了什么?自动生成的文件隐藏在
obj\Debug\netcoreapp1.1\YourProject.AssemblyInfo.cs
我确实看到了它,是的,它仍然是1.0.0.0。这是netcoreapp2.0吗?
<PropertyGroup>
  <Version>1.1.0</Version>
  <AssemblyVersion>1.1.0.9</AssemblyVersion>
  <FileVersion>1.1.0.9</FileVersion>
</PropertyGroup>
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;
}