简体   繁体   English

如何自动将解决方案中的所有项目设置为相同版本?

[英]How do I automatically set all projects in my solution to the same version?

I have a Visual Studio solution with 5 C# projects.我有一个包含 5 个 C# 项目的 Visual Studio 解决方案。 I want the assembly versions to match in each project.我希望程序集版本在每个项目中都匹配。 But it looks like I have to go into each project's properties and click assembly version for each project one at a time.但看起来我必须将 go 放入每个项目的属性中,然后一次单击每个项目的程序集版本。 Is there a way to treat the solution like it just has one version as a whole that applies to each project?有没有办法将解决方案视为只有一个适用于每个项目的整体版本?

Just create a file eg GlobalAssemblyInfo.cs in the solution root folder then add the necessary attributes to it and finally add it as an existing item to each project as a link.只需在解决方案根文件夹中创建一个文件,例如GlobalAssemblyInfo.cs ,然后向其中添加必要的属性,最后将其作为现有项目作为链接添加到每个项目中。

In Solution Explorer right click on the project name > Add > Existing item... and in the dialog box select Add As Link option from the dropdown list as you can see on this image .Solution Explorer右键单击project name > Add > Existing item...然后在对话框中从下拉列表中选择Add As Link选项,如您在此图像中所见。

// Content of GlobalAssemblyInfo.cs file
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Please note:请注意:

  • you have to remove these attributes from each project's Properties\\AssemblyInfo.cs file.您必须从每个项目的Properties\\AssemblyInfo.cs文件中删除这些属性。
  • you can also move other assembly attributes into the GlobalAssemblyInfo.cs file as well您也可以将其他程序集属性移动到GlobalAssemblyInfo.cs文件中

The result is that you will have only one file where you can set the version and it will apply to all projects.结果是您将只有一个可以设置版本的文件,它将适用于所有项目。

UPDATE #1:更新#1:

In .NET 5 projects an AssemblyInfo.cs file is automatically generated during build, by default..NET 5项目中,默认情况下,在构建期间会自动生成AssemblyInfo.cs文件。

It seems that only 7 attributes is generated automatically:好像只有7个属性是自动生成的:

  • AssemblyCompanyAttribute
  • AssemblyProductAttribute
  • AssemblyConfigurationAttribute
  • AssemblyVersionAttribute
  • AssemblyFileVersionAttribute
  • AssemblyInformationalVersionAttribute
  • AssemblyTitleAttribute

You have two options here:您在这里有两个选择:

  • Disable automatic generation of AssemblyInfo.cs file.禁用AssemblyInfo.cs文件的自动生成。
  • Leave automatic generation of AssemblyInfo.cs file enabled and turn off generation of specific attributes.启用AssemblyInfo.cs文件的自动生成并关闭特定属性的生成。

Create a file (name: Directory.Build.props ) and put it next to the .sln file so that it will be applied to all the projects within the solution.创建一个文件(名称: Directory.Build.props )并将其放在.sln文件旁边,以便将其应用于解决方案中的所有项目。

Example #1 - Disable automatic build of AssemblyInfo.cs file示例 #1 - 禁用AssemblyInfo.cs文件的自动构建

Directory.Build.props : Directory.Build.props :

<Project>
  <PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
</Project>

Example #2 - Disable only specific attribute generation示例 #2 - 仅禁用特定属性生成

In this case simply add <Generate...>false</Generate...> line to disable a specific attribute where ... is the attribute type name.在这种情况下,只需添加<Generate...>false</Generate...>行以禁用特定属性,其中...是属性类型名称。

Directory.Build.props : Directory.Build.props :

<Project>
  <PropertyGroup>
    <GenerateAssemblyInfo>true</GenerateAssemblyInfo>
    <GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
    <GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
    <GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
  </PropertyGroup>
</Project>

Remarks备注

Learn more about AssemblyInfo properties in SDK-style project files.了解有关 SDK 样式项目文件中的AssemblyInfo 属性的更多信息。

This update applies to .NET Core versions as well.此更新也适用于 .NET Core 版本。

If a specific project has special needs you can override these settings in the .csproj file as well.如果特定项目有特殊需求,您也可以覆盖.csproj文件中的这些设置。

As for me I usually put the attributes as follows:至于我,我通常把属性如下:

  • GlobalAssemblyInfo.cs
    • AssemblyCompanyAttribute
    • AssemblyProductAttribute
    • AssemblyCopyrightAttribute
    • AssemblyConfigurationAttribute
    • AssemblyTrademarkAttribute
    • AssemblyCultureAttribute
    • AssemblyVersionAttribute
    • AssemblyFileVersionAttribute
    • AssemblyInformationalVersionAttribute
    • ComVisibleAttribute
  • AssemblyInfo.cs (in specific projects) AssemblyInfo.cs (在特定项目中)
    • AssemblyTitleAttribute
    • AssemblyDescriptionAttribute
    • GuidAttribute

I don't think there's any solution level option to do this.我认为没有任何解决方案级别的选项可以做到这一点。 I use powershell script to achieve it for my 15 projects in a solution.我使用 powershell 脚本在一个解决方案中为我的 15 个项目实现它。

  $version= "1.3.0.0" 
  (Get-ChildItem -Include AssemblyInfo.cs -Recurse ) | 
     Foreach-Object { 
         Set-Content $_ ((Get-content $_ -Encoding UTF8) -replace "\d+\.\d+\.(\d+|\*)(\.(\d+|\*))?", $version)  -Encoding UTF8 
    }

Save this script with in same directory as your solution file.将此脚本保存在与解决方案文件相同的目录中。 You can also add this as solution item in the solution itself and launch it from Visual studio command line option when you right click on the script file.您还可以将其添加为解决方案本身中的解决方案项,并在您右键单击脚本文件时从 Visual Studio 命令行选项启动它。

Since each project has its own assembly they will be treated independently.由于每个项目都有自己的程序集,因此它们将被独立处理。 What I've found works best for me is to use a T4 template and base it on an algorithm within the template such as this where the revision is a calculation for hours since 1/1/2000:我发现最适合我的是使用 T4 模板并基于模板中的算法,例如这样的修订是自 2000 年 1 月 1 日以来的几个小时的计算:

<#@ template debug="false" hostspecific="false" language="C#" #>
using System.Reflection;
[assembly: AssemblyVersion("1.0.6.<#= this.RevisionNumber #>")]
[assembly: AssemblyFileVersion("1.0.6.<#= this.RevisionNumber #>")]
<#+
    int RevisionNumber = (int)(DateTime.UtcNow - new DateTime(2000,1,1)).TotalHours;
#>

Since MSBuild 15 (Visual Studio 2017) ( https://docs.microsoft.com/en-us/visualstudio/msbuild/what-s-new-in-msbuild-15-0 ) you can use Directory.Build.targets file for this.由于 MSBuild 15 (Visual Studio 2017) ( https://docs.microsoft.com/en-us/visualstudio/msbuild/what-s-new-in-msbuild-15-0 ) 你可以使用Directory.Build.targets文件为了这。

  1. Create Directory.Build.targets file where your.sln file is在 your.sln 文件所在的位置创建Directory.Build.targets文件
  2. Add your version to the created file:将您的版本添加到创建的文件中:
  <Project>
    <PropertyGroup>
        <Version>0.1.0-alpha</Version>
    </PropertyGroup>
  </Project>
  1. That's it - this will get applied to all projects in the solution.就是这样 - 这将应用于解决方案中的所有项目。

You can do many other things with Directory.Build.props and Directory.Build.targets - more here: ( https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build )您可以使用 Directory.Build.props 和 Directory.Build.targets 做许多其他事情 - 更多内容:( https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将同一解决方案中的 2 个项目推送到我的 Github 存储库? - How do push 2 projects in the same solution to my Github repository? 如何在Microsoft Azure的同一解决方案中引用项目 - How do I reference projects in the same solution in Microsoft Azure 如何以编程方式列出解决方案中的所有项目? - How do I programmatically list all projects in a solution? 如何从解决方案中的所有项目中卸载* a *软件包 - How do I uninstall *a* package from all projects in solution 如何使用EnvDTE列出当前解决方案中的所有项目? - How do I list all the projects in the current solution using EnvDTE? 如何在一个地方为我的解决方案中的所有项目更改 C# 语言版本? - How to change C# Language Version for all of the projects in my solution in one place? 如何在夜间构建期间自动设置装配版本? - How do I automatically set assembly version during nightly build? 如何在Visual Studio 2012中切换(或突出显示)同一解决方案的项目? - How do i switch between (or highlight) projects of the same solution in Visual Studio 2012? Reportviewer和报表位于同一解决方案的不同项目中。 如何将报告附加到查看器 - Reportviewer and report are in different projects in the same solution. How do I attach the report to the viewer 如何在解决方案中设置所有项目以复制本地false - how to set all projects in solution to copy local false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM