[c#] Compiling/Executing a C# Source File in Command Prompt

How do you compile and execute a .cs file from a command-prompt window?

This question is related to c# command-line compilation command-prompt

The answer is


You can compile a C# program :

c: > csc Hello.cs

You can run the program

c: > Hello


Add to path

C:\Windows\Microsoft.NET\Framework\v3.5

To Compile:

csc file.cs

To Execute:

file

For the latest version, first open a Powershell window, go to any folder (e.g. c:\projects\) and run the following

# Get nuget.exe command line
wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile nuget.exe

# Download the C# Roslyn compiler (just a few megs, no need to 'install')
.\nuget.exe install Microsoft.Net.Compilers

# Compiler, meet code
.\Microsoft.Net.Compilers.1.3.2\tools\csc.exe .\HelloWorld.cs

# Run it
.\HelloWorld.exe    

An example HelloWorld.cs

using System;

public class HelloWorld {
    public static void Main() 
    {
        Console.WriteLine("Hello world!");
    }
}

You can also try the new C# interpreter ;)

.\Microsoft.Net.Compilers.1.3.2\tools\csi.exe
> Console.WriteLine("Hello world!");
Hello world!

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn

this is where you can find the c# compiler that supports c#7 otherwise it will use the .net 4 compilers which supports only c# 5


If you have installed Visual Studio then you have Developer Command Prompt for VS. You can easily build your program using csc command and run your application with the name of the application inside the developer command prompt.

You can open Developer command prompt as given below.

Start => Developer Command Prompt for VS

Hope this helps!


Once you write the c# code and save it. You can use the command prompt to execute it just like the other code.

In command prompt you enter the directory your file is in and type

To Compile:

mcs yourfilename.cs

To Execute:

mono yourfilename.exe 

if you want your .exe file to be different with a different name, type

To Compile:

mcs yourfilename.cs -out:anyname.exe 

To Execute:

mono anyname.exe 

This should help!


You can build your class files within the VS Command prompt (so that all required environment variables are loaded), not the default Windows command window.

To know more about command line building with csc.exe (the compiler), see this article.


While it is definitely a good thing knowing how to build at the command line, for most work it might be easier to use an IDE. The C# express edition is free and very good for the money ;-p

Alternatively, things like snippy can be used to run fragments of C# code.

Finally - note that the command line is implementation specific; for MS, it is csc; for mono, it is gmcs and friends.... Likewise, to execute: it is just "exename" for the MS version, but typically "mono exename" for mono.

Finally, many projects are build with build script tools; MSBuild, NAnt, etc.


LinqPad is a quick way to test out some C# code, and its free.


Microsoft has moved its compiler to Github (ofcourse):

https://github.com/dotnet/roslyn


Another way to compile C# programs (without using Visual Studio or without having it installed) is to create a user variable in environment variables, namely "PATH".

Copy the following path in this variable:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319"

or depending upon which .NET your PC have.

So you don't have to mention the whole path every time you compile a code. Simply use

"C:\Users\UserName\Desktop>csc [options] filename.cs"

or wherever the path of your code is.

Now you are good to go.


In Windows systems, use the command csc <filname>.cs in the command prompt while the current directory is in Microsoft Visual Studio\<Year>\<Version>

There are two ways:

Using the command prompt:

  1. Start --> Command Prompt
  2. Change the directory to Visual Studio folder, using the command: cd C:\Program Files (x86)\Microsoft Visual Studio\2017\ <Version>
  3. Use the command: csc /.cs

Using Developer Command Prompt :

  1. Start --> Developer Command Prompt for VS 2017 (Here the directory is already set to Visual Studio folder)

  2. Use the command: csc /.cs

Hope it helps!


Here is how to install MSBuild with standalone C# 7.0 compiler which is no longer bundled in the latest .Net Framework 4.7:

Is it possible to install a C# compiler without Visual Studio?

Then just run

"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\Roslyn\csc.exe" MyApplication.cs

to compile single source file to executable.

Also note that .Net Core doesn't support compiling single source file without preconfigured project.


I have written a batch file CompileCS.cmd which allows to compile (and optionally run) a C# file, you can find the complete answer including the batch program 'CompileCS' here.

Its usage is simple:

CompileCS /run GetDotNetVersion.cs[arg1] ... [arg9]

will compile and run GetDotNetVersion.cs - if you just want to compile it, omit the /run parameter.

The console application GetDotNetVersion.cs is taken from this answer.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)

Examples related to compilation

WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' How to enable C++17 compiling in Visual Studio? How can I use/create dynamic template to compile dynamic Component with Angular 2.0? Microsoft Visual C++ Compiler for Python 3.4 C compile : collect2: error: ld returned 1 exit status Error:java: invalid source release: 8 in Intellij. What does it mean? Eclipse won't compile/run java file IntelliJ IDEA 13 uses Java 1.5 despite setting to 1.7 OPTION (RECOMPILE) is Always Faster; Why? (.text+0x20): undefined reference to `main' and undefined reference to function

Examples related to command-prompt

CMD command to check connected USB devices How do I kill the process currently using a port on localhost in Windows? PowerShell The term is not recognized as cmdlet function script file or operable program open program minimized via command prompt How to connect to SQL Server from command prompt with Windows authentication How to see the proxy settings on windows? How do I type a TAB character in PowerShell? Batch file to split .csv file Aliases in Windows command prompt Change all files and folders permissions of a directory to 644/755