[英]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:请注意:
Properties\\AssemblyInfo.cs
file.Properties\\AssemblyInfo.cs
文件中删除这些属性。GlobalAssemblyInfo.cs
file as wellGlobalAssemblyInfo.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:您在这里有两个选择:
AssemblyInfo.cs
file.AssemblyInfo.cs
文件的自动生成。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
文件为了这。
Directory.Build.targets
file where your.sln file isDirectory.Build.targets
文件 <Project>
<PropertyGroup>
<Version>0.1.0-alpha</Version>
</PropertyGroup>
</Project>
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.