33

I'm fairly new to C# and I'm trying to use cmd to compile a basic hello world file called test.cs. It contains the following:

// 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
     */

When I try to run the line csc test.cs I get the following output: img of issue

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
Callat
  • 2,928
  • 5
  • 30
  • 47
  • 1
    is csc part of your environment variables? probably the compiler is not in your desktop (see the route in which you're executing the command). You might want to search where the compiler is and run it from there – Gonzalo.- Mar 28 '17 at 21:34
  • how would i find my environment variables? I'm a little familiar with terminal on ubuntu but I'm new to cmd – Callat Mar 29 '17 at 01:57
  • 1
    Run `"%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\csc.exe" test.cs` instead of just `csc test.cs`, i.e. executable `csc` with file extension and with full path enclosed in double quotes to always work. The path depends on which .NET framework you want to use for compilation. – Mofi Mar 29 '17 at 05:34

1 Answers1

54

Locate the path of csc.exe and add it your PATH environment variable.

In my case, the path for 64-bit C# compiler is C:\Windows\Microsoft.NET\Framework64\v4.0.30319.

Similarly, you can look for 32-bit C# compiler in C:\Windows\Microsoft.NET\Framework under different .NET framework version directories

There will be csc.exe for all versions like v2.0.XXXXX and v3.5. Select the one with the highest version in Framework64/Framework directory depending on your requirement.

Copy the path of csc.exe and add it to the PATH system environment variable.

Quit the cmd, and then launch again and run the program. That'd work.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • 2
    how would i add that to an environment variable? I'm brand new to cmd so I don't know what that is – Callat Mar 29 '17 at 01:58
  • 2
    @KazRodgers - Check this link -> https://www.java.com/en/download/help/path.xml. It shows how to edit environment variable `PATH` for different OS. Follow the steps very closely as you may commit some mistake. – Am_I_Helpful Mar 29 '17 at 04:58
  • 1
    This compiler doesn't work for c sharp after version 5. – Pavindu Apr 01 '19 at 17:40
  • Do we include the `csc.exe` in the path value? – Marvin Mar 29 '20 at 21:26
  • @Marvin - No, we don't include the exe name in the path value, only up to the directory inside which the executable needs to be searched. – Am_I_Helpful Mar 30 '20 at 03:45
  • simple instruction how set environment variable https://www.softwareok.com/?seite=faq-Windows-10&faq=10 – Adam G. Oct 22 '22 at 23:55