Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a Visual Studio solution with 5 C# projects. 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. Is there a way to treat the solution like it just has one version as a whole that applies to each project?

like image 359
Kyle Delaney Avatar asked Sep 21 '17 20:09

Kyle Delaney


2 Answers

Just create a file e.g. 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.

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.

// 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.
  • you can also move other assembly attributes into the GlobalAssemblyInfo.cs file as well

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:

In .NET 5 projects an AssemblyInfo.cs file is automatically generated during build, by default.

It seems that only 7 attributes is generated automatically:

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

You have two options here:

  • Disable automatic generation of AssemblyInfo.cs file.
  • Leave automatic generation of AssemblyInfo.cs file enabled and turn off generation of specific attributes.

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.

Example #1 - Disable automatic build of AssemblyInfo.cs file

Directory.Build.props:

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

Example #2 - Disable only specific attribute generation

In this case simply add <Generate...>false</Generate...> line to disable a specific attribute where ... is the attribute type name.

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.

This update applies to .NET Core versions as well.

If a specific project has special needs you can override these settings in the .csproj file as well.

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)
    • AssemblyTitleAttribute
    • AssemblyDescriptionAttribute
    • GuidAttribute
like image 123
Gabor Avatar answered Oct 17 '22 16:10

Gabor


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.

  $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.

like image 3
vendettamit Avatar answered Oct 17 '22 14:10

vendettamit