Compiling/Executing a C# Source File in Command Prompt
How do you compile and execute a .cs file from a command-prompt window?
CSC.exe is the CSharp compiler included in the .NET Framework and can be used to compile from the command prompt. The output can be an executable ".exe", if you use "/target:exe", or a DLL; If you use /target:library, CSC.exe is found in the .NET Framework directory,
e.g. for .NET 3.5, c:\windows\Microsoft.NET\Framework\v3.5\
.
To run, first, open a command prompt, click "Start", then type cmd.exe
.
You may then have to cd into the directory that holds your source files.
Run the C# compiler like this:
c:\windows\Microsoft.NET\Framework\v3.5\bin\csc.exe
/t:exe /out:MyApplication.exe MyApplication.cs ...
(all on one line)
If you have more than one source module to be compiled, you can put it on that same command line. If you have other assemblies to reference, use /r:AssemblyName.dll
.
Ensure you have a static Main() method defined in one of your classes, to act as the "entry point".
To run the resulting EXE, type MyApplication
, followed by <ENTER>
using the command prompt.
This article on MSDN goes into more detail on the options for the command-line compiler. You can embed resources, set icons, sign assemblies - everything you could do within Visual Studio.
If you have Visual Studio installed, in the "Start menu"; under Visual Studio Tools, you can open a "Visual Studio command prompt", that will set up all required environment and path variables for command line compilation.
While it's very handy to know of this, you should combine it with knowledge of some sort of build tool such as NAnt, MSBuild, FinalBuilder etc. These tools provide a complete build environment, not just the basic compiler.
On a Mac
On a Mac, syntax is similar, only C sharp Compiler is just named csc
:
$ csc /target:exe /out:MyApplication.exe MyApplication.cs ...
Then to run it :
$ mono MyApplication.exe
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.
You can compile a C# program :
c: > csc Hello.cs
You can run the program
c: > Hello
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!