Visual Studio 2022 中 .Net Core 6 解决方案版本信息的中心位置

Gor*_*onS 4 visual-studio asp.net-core-6.0 visual-studio-2022

对于使用 Visual Studio 2019 的 ASP.Net,我创建了一个共享的 AssemblyInfo.cs 文件,然后在解决方案的每个项目中引用该文件。从而为我提供了一个位置来设置版本信息,然后为每个项目设置该版本信息。

\n
using System.Reflection;\nusing System.Runtime.InteropServices;\nusing JetBrains.Annotations;\n\n[assembly: AssemblyConfiguration("")]\n[assembly: AssemblyCompany("Company Name")]\n[assembly: AssemblyProduct("Product name")]\n[assembly: AssemblyCopyright("Copyright \xc2\xa9 Company. All Rights Reserved.")]\n[assembly: AssemblyTrademark("")]\n[assembly: AssemblyCulture("")]\n\n// Setting ComVisible to false makes the types in this assembly not visible \n// to COM components.  If you need to access a type in this assembly from \n// COM, set the ComVisible attribute to true on that type.\n[assembly: ComVisible(false)]\n\n[assembly: AssemblyVersion("1.15.0")]\n[assembly: AssemblyFileVersion("1.15.0.7156")]\n[assembly: AssemblyInformationalVersion("1.15.0")]\n
Run Code Online (Sandbox Code Playgroud)\n

每个项目都有一个简化的 AssemblyInfo.cs 文件:

\n
using System.Reflection;\nusing System.Runtime.InteropServices;\n\n// General Information about an assembly is controlled through the following \n// set of attributes. Change these attribute values to modify the information\n// associated with an assembly.\n[assembly: AssemblyTitle("ProjectName")]\n[assembly: AssemblyDescription("")]\n// The following GUID is for the ID of the typelib if this project is exposed to COM\n[assembly: Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]\n
Run Code Online (Sandbox Code Playgroud)\n

共享的 AssemblyInfo.cs 文件作为链接包含在每个项目中。

\n

我正在使用 Visual Studio 2022 和 .Net Core 6 开始一个新的解决方案。我看到我可以在每个项目的 csproj 中设置版本、AssemblyVersion 和 FileVersion ...但我不知道如何拥有一个中央像我在之前的解决方案中所做的那样设置值。

\n

我已经看到我可以恢复手动创建 AssemblyInfo.cs 文件,但是有没有办法在不禁用当前标准方法(在 VS2022 / .Net Core 6 中)的情况下执行我想要的操作?

\n

代码处于源代码控制 (SVN) 之下,但我们(当前)不使用任何构建或 CI 工具。我只是希望能够将版本设置为特定值,并使其对于解决方案中的所有项目都相同。我很高兴我可以打开每个 csproj 文件并更新它们,但我宁愿有一个位置。

\n

Jim*_*mmy 7

您可以使用 MSBuild 导入在每个 csproj 中包含一个通用文件。例如,创建一个包含所有共享属性的文件:

<Project>
  <PropertyGroup>
    <AssemblyVersion>1.15.0</AssemblyVersion>
    <AssemblyFileVersion>1.15.0.7156</AssemblyFileVersion>
    <!-- etc... -->
  </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

然后您可以<Import Project="CommonAssemblyProperties.props" />在每个项目文件中(或任何您命名的文件)。

如果您想自动导入,可以将其放入解决方案根目录下的 Directory.Build.props 文件中,因为该文件会自动导入(您甚至可以在此处分配属性,而不必使用<Import>额外的文件) )。

如果需要,此方法还允许您在构建期间覆盖属性值(例如,如果您希望 CI 构建与开发构建不同)msbuild MySolution.sln /p:Property=Value,或者在单个项目中通过在 csproj 中设置属性值(例如,如果一个项目具有与其他版本不同的特殊版本)。