C# 环境变量

将路径 C:\Windows\Microsoft.NET\Framework\v4.0.30319 添加到系统环境变量 Path 中。

然后打开命令行终端执行命令 csc,如图:

C# 环境变量

我们可以看到 C# 编译器(csc.exe)的版本号,说明环境变量配置成功了!

但是下面提示:

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

大概的意思就是说我们配置的环境变量路径中的 C# 编译器版本太低了,需要进行升级!

那么问题来了,我在 VS 中是可以编译 C# 的,为啥自己配置环境变量就不行了呢?

C# 环境变量

在网上查了一下,原来是我安装 VS 的时候修改了默认安装路径,导致最新版本的 C# 编译器并没有安装到这个默认路径(C:\Windows\Microsoft.NET\Framework\)中,而目前这个路径下面版本最高的就是 v4.0.30319 了。

所以只需要在当前 VS 的安装路径中找到 C# 编译器所在的路径(C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn),将其替换掉之前的路径就可以了!

C# 环境变量