【问题标题】:'csc' is not recognized as an internal or external command, operable program or batch file [duplicate]“csc”不是内部或外部命令、可运行程序或批处理文件[重复]
【发布时间】:2017-08-22 03:53:19
【问题描述】:

我是 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 行时,我得到以下输出:

【问题讨论】:

  • csc 是您环境变量的一部分吗?可能编译器不在您的桌面中(请参阅执行命令的路径)。您可能想搜索编译器的位置并从那里运行它
  • 如何找到我的环境变量?我对 ubuntu 上的终端有点熟悉,但我是 cmd 的新手
  • 运行"%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\csc.exe" test.cs 而不是仅仅运行csc test.cs,即带有文件扩展名的可执行文件csc 并用双引号括起来的完整路径始终有效。路径取决于您要用于编译的 .NET 框架。

标签: c# cmd path environment-variables csc


【解决方案1】:

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

在我的例子中,64 位 C# 编译器的路径是 C:\Windows\Microsoft.NET\Framework64\v4.0.30319

同样,您可以在不同的.NET framework 版本目录下的C:\Windows\Microsoft.NET\Framework 中寻找32位C#编译器

v2.0.XXXXX 和 v3.5 等所有版本都会有 csc.exe。根据您的要求选择Framework64/Framework目录中版本最高的那个。

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

退出 cmd,然后再次启动并运行程序。可以的。

【讨论】:

  • 如何将它添加到环境变量中?我是 cmd 的新手,所以我不知道那是什么
  • @KazRodgers - 检查此链接 -> java.com/en/download/help/path.xml。它展示了如何为不同的操作系统编辑环境变量PATH。请严格按照步骤操作,因为您可能会犯一些错误。
  • 此编译器在版本 5 之后不适用于 c sharp。
  • 我们是否在路径值中包含csc.exe
  • @Marvin - 不,我们不在路径值中包含 exe 名称,只包含需要搜索可执行文件的目录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2018-08-25
  • 2014-06-01
  • 2022-01-06
相关资源
最近更新 更多