“csc” 未被识别为内部或外部命令、可执行程序或批处理文件。

33

我对C#比较陌生,现在想要用cmd编译一个名为test.cs的基本hello world文件。它包含以下内容:

// Similar to #include<foo.h> in C++, includes system namespaces in a program

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    // A name space declaration, a class is a group of namespaces
    namespace Program1
    {
        class Hello // my class here, classes can contain multiple functions called methods which define it's behavior
        {
            static void Main(string[] args) // main method, just like in C/C++ it's the starting point for execution cycle
            {
                Console.WriteLine("Hello World");
                Console.ReadKey(); // similar to _getch() in C++ waits for user to input something before closing
            }
        }
    }

    /*
     * Other notes, .cs is a c# file extension
     * cs files can be built via terminal by using csc foo.cs to generate foo.exe run it with foo
     */

当我尝试运行该行csc test.cs时,我得到以下输出:

img of issue

1
CSC 是否在您的环境变量中?很可能编译器不在您的桌面上(请查看执行命令的路径)。您可能需要搜索编译器所在的位置并从那里运行它。 - Gonzalo.-
我该如何找到我的环境变量?我对Ubuntu终端有一点了解,但是我对cmd还不熟悉。 - Callat
1
请运行"%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\csc.exe" test.cs,而不是仅使用csc test.cs。也就是说,将可执行文件csc与文件扩展名一起用双引号括起来,并附上完整路径,以确保始终正常工作。路径取决于您要使用哪个.NET框架进行编译。 - Mofi
1个回答

56

找到 csc.exe 的路径,并将其添加到您的 PATH 环境变量中。

在我的情况下,64位的C#编译器路径为:C:\Windows\Microsoft.NET\Framework64\v4.0.30319

同样地,您可以在不同 .NET 框架版本目录下查找32位的C#编译器,例如在 C:\Windows\Microsoft.NET\Framework 中寻找。对于所有版本(如v2.0.XXXXX和v3.5)都将有csc.exe可用。根据您的需求,在 Framework64/Framework 目录中选择具有最高版本的一个。

复制 csc.exe 的路径并将其添加到 PATH 系统环境变量中。

退出 cmd 窗口,然后重新启动它并运行程序。这样就可以了。


3
我该如何将其添加到环境变量中?我对cmd完全不熟悉,所以不知道那是什么。 - Callat
2
@KazRodgers - 请查看此链接 -> https://www.java.com/en/download/help/path.xml。它展示了如何编辑不同操作系统的环境变量`PATH`。请非常仔细地按照步骤操作,以免出错。 - Am_I_Helpful
1
这个编译器在c# 5版本之后就无法工作了。 - Pavindu
我们在路径值中包含 csc.exe 吗? - Marvin
@Marvin - 不,我们在路径值中不包含exe文件名,只包括需要搜索可执行文件的目录。 - Am_I_Helpful
简单指南:如何设置环境变量 - Adam G.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接