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

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:

<#@ 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;
#>

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.


Since MSBuild 15 Visual Studio 2017 you can use Directory.Build.targets file for this.

  1. Create Directory.Build.targets file where your .sln file is
  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


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