44

I have a solution with 20 projects, I want to change the C# version for all of them to C# 7.3

Is there a way that I could change all project's version at one go? I know I can change the Language Version from Project's Properties, but I don't want to repeat this 20 times.

enter image description here

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
  • 1
    If possible - you could also upgrade your Visual Studio to 2019 where [default language versions](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version) are set as what you want. – weichch Apr 26 '20 at 03:04
  • I would suggest to change one project and compare the changed project with the original one. Then you can perform a find and replace in files to replace all other files. Notepad++ has an option to find and replace in files. – Kaveesh Apr 26 '20 at 06:02
  • Note that newer versions of Visual Studio do not allow you to specify the language version via this UI. The language version is determined based upon the target framework. You can still override that using `LangLevel` in the project file, as shown in the answers below. – Drew Noakes May 28 '21 at 00:34
  • @DrewNoakes Well you can TRY to override/fix it but for me it does not seem to work...must be some complex mystery of things that keeps mine thinking it is c# 5 grrr on VS here. – Mark Schultheiss Nov 22 '22 at 17:15
  • @MarkSchultheiss what version of VS are you using? So long as the VS version is new enough, you can override the `LangLevel` in your project file manually (or in a `Directory.Build.props` file at the solution level to apply to all projects). – Drew Noakes Dec 03 '22 at 06:43
  • @DrewNoakes VS 2022 for me; "override the LangLevel in your project file manually" - yea, editing over 150 project files is IMHO a fail by VS. I did get this to work but ended up removing and re-adding some packages and removing and forcing VS to re-add assembly related web.config values to get the version updates and to force the proper compiler update to support the latest c# version. NOTE: This dropdown illustrated is disabled: ("Automatically selected based on framework version") – Mark Schultheiss Dec 05 '22 at 15:02
  • @MarkSchultheiss you can edit the `Directory.Build.props` file to set this once for all projects. You don't have to edit every project manually. Unfortunately there isn't a way to do this via the GUI in VS. See https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2022#directorybuildprops-and-directorybuildtargets for more info. – Drew Noakes Dec 14 '22 at 03:22
  • @DrewNoakes Thanks for the comment, I did know about this one and ran a test on the optio previously. Suffice to say "it's complicated" for me here as the 160+ just touches the surface here (some have other projects under that level) and has to be done in "blocks" due to other challenges. The full "application" is non-trivial and is really composed of many more projects and solutions that come into play as "one". nearly 300GB of "source" as projects/solutions and another 450GB in ~7400 "source files" that comes into play here in a bit more dynamic way does that to you. – Mark Schultheiss Dec 14 '22 at 13:02

3 Answers3

49

To set a version for all your project at once, you can create a file named Directory.Build.props (case-sensitive on Linux) at the root of your repository. This file contains the list of common properties of your projects:

<Project>
  <PropertyGroup>
    <LangVersion>latest</LangVersion>
    <!--<LangVersion>preview</LangVersion>-->
    <!--<LangVersion>7.3</LangVersion>-->
  </PropertyGroup>
</Project>

https://www.meziantou.net/4-ways-to-enable-the-latest-csharp-features.htm#method-4-using-a-pro

meziantou
  • 20,589
  • 7
  • 64
  • 83
  • 5
    Unfortunately intellisense doesn't quite work with the `Directory.Build.props` method. It is showing a lot of errors in my solution files. Hope I can find a solution. – johnildergleidisson Mar 25 '21 at 20:09
  • Apparently in old SDK projects you likely need `ToolsVersion="16.0"`. See https://stackoverflow.com/questions/53633538/how-to-enable-nullable-reference-types-feature-of-c-sharp-8-0-for-the-whole-proj#comment103405676_56267555 – Daniel Lidström Jun 09 '21 at 08:25
  • I can only wish it were this simple - kind of a frustration point with VS (2022 v17+ in my case) when you have hundreds of projects in a solution and settings like this do not seem to work/propagate to all the projects and you waste huge amounts of time trying to find a solution – Mark Schultheiss Nov 22 '22 at 17:13
  • @MarkSchultheiss are you using `Directory.Build.props`? Be aware that MSBuild searches up the directory tree for these files and stops when it finds the nearest one. If you need to chain multiple, you must have each deeper one import the next higher one. – Drew Noakes Dec 03 '22 at 06:45
19

The accepted answer is great, I am just adding some details.

I created Directory.Build.props file at the root of the of the repository and added it to the solution as a Solution Item. This is the content of the file:

<Project>
  <PropertyGroup>
    <LangVersion>7.3</LangVersion>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
  </PropertyGroup>
</Project>

Note 1:

If you modify these setting, you need to restart Visual Studio for the changes to take effect.

Note 2:

You would require MSBuild version 15 or higher in order to use Directory.Build.props. To check MSBuild version, open your project file in a text editor and look for the following:

<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Here, 15.0 indicates your MSBuild version, see this document if you require to upgrade your MSBuild version.

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
-3

I am fairly new to using ASP.Net applications so this is a bit of a newb fix

I got this error when migrating my application from ASP.Net Core Web App to ASP.Net Core Web API.

Moving my class libraries outside the main folder that contains the .sln file fixed this error for me.