简体   繁体   中英

How to find and use the latest version of CSC.exe when using Visual Studio Code?

I'm setting up Visual Studio Code to write and learn C#.

I'm not able to find and use the latest version of C# compiler with Visual Studio Code.

Enquiring my compiler version with the VSC terminal command csc -langversion:? brings up the following error message:

csc : The term 'csc' is not recognized as the name of cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.


My previous attempts in solving the problem

Having searched intensely I have found out this:

  • When Using Visual Studio this problem doesn't even occur. VS apparently contains the necessary .NET SDK and compiler and uses it automatically.
  • I should locate the csc.exe file and set it as an environment variable path.

The only csc.exe files I find are located on the directory: C:\\Windows\\Microsoft.NET\\Framework64\\v xxxxxxx with the latest being version being 4.0.30319 .

All the instructions I could find suggested using this folder to be set as an environment variable path.

I do that. I now try again on VSC: csc -langversion:?

Now I get the following message:

Microsoft (R) Visual C# Compiler version 4.8.4084.0 for C# 5 Copyright (C) Microsoft Corporation. All rights reserved.

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

warning CS2008: No source files specified error CS1617: Invalid option '?'for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default"

So the csc.exe I was referring and have set as an environment variable path is outdated. I would, however, like to get all the modern features of C# 9.

I tried again to find the latest compiler from the folder "C:\\Program Files\\dotnet" but I could only find a folder "Roslyn", which to my understanding refers to the newest versions of csc. I couldn't find any csc.exe from these folders.


The solutions?

Now should I find the latest csc.exe supporting C# 9 from somewhere (where?) and set it up as an environmental variable path?

Or am I on the wrong track here?

Or is the only choice that I give up VSC completely and switch to Visual Studio (I would't want to do that). But that can't be the only solution!


Background information

I am a total beginner to the world of coding. Have mercy.

I have installed the latest version of .NET SDK 5.0.302.

I have installed the following extensions on my VSC: C# (MS), .NET Install Tool for Extension Authors (MS), C# XML Documentation Comments (Keisuke Kato), ilspy-vscode (SharpDevelop Team), MSBuild project tools (tintoy), REST Client (Huachao Mao)

You should use dotnet command in your console to build/run .Net 5 app.

Try to install C# extension to you VS Code.

Just adding my last piece of findings, which I find crucial.

Using statement:

#error version

produces detailed info about the compiler in use. It returned the same version number for the compiler than if I wouldn't have defined the Roslyn folder under the .vscode as PATH on environment variables.

Therefore I think I can safely conclude this:

  • VSC doesn't show you the current compiler with the call csc -langversion:? unless you add the aforementioned Roslyn folder under .vscode as a PATH on environmental variables.
  • However, it does seem that VSC uses this particular compiler anyway , even though it doesn't formally regocnize it. So only thing that is really important is to install the latest .NET SDK.

I did the following and it worked.

I went to my computer>> properties>> advanced system settings>>enviroment variables>> double click on "Path" >> Add new>> Paste the directory where you can find csc.exe and click ok. i restarted VSC and it worked. The directory where I found csc.exe for me was >> C:\\Program Files (x86)\\Microsoft SDKs\\UWPNuGetPackages\\microsoft.net.native.compiler\\2.2.10-rel-29722-00\\tools\\csc

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