简体   繁体   中英

How to change C# Language Version for all of the projects in my solution in one place?

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.

在此处输入图像描述

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM