Most people use Visual Studio to run their C# programs, and don't know that they can achieve the same result by using the developer command prompt. The developer cmd prompt can be found in C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts, this is for visual studio 2013. The benefits of the developer cmd prompt from the regular windows cmd prompt is that it has been pre-configured to provide you with all of the necessary .NET tools needed.

Why would I ever want to use the command prompt to build my applications when I have an IDE?

  • If you are using planning to use an automated build tool like msbuild.exe then knowing the command line options are necessary.
  • Visual Studio runs the csc.exe in the background when you compile your code. Learning how to use the command line options greatly increase your knowledge of the C# compiler and how it works.
  • Using the cmd is a lot more lightweight than using a graphical IDE.
  • Other .NET utilities such as gacutil.exe, ngen.exe, ilasm.exe, and aspnet_regiis.exe are only accessible from the command line.

The basic command is csc -? (csc stands for c sharp compiler) which shows all the commands available.

CSC

From this huge list, you would only be using a handful of commands.

Lets start our first command line C# program named hello.cs using a text editor:

1
2
3
4
5
6
7
8
9
Using System;

public class Hello
{
    public static void Main()
    {
       Console.WriteLine("Hello World");
    }
}

Run the command csc /target:exe hello.cs and then execute hello.exe.

csc2

The /target:exe  (can be shortened to /t:exe ) is for telling the compiler to make an .exe  file. Then running the hello.exe binary will execute your program.

This is the basics of running your C# program from the command line. There are a lot more complexities that can be added, we will see those in action in later blog posts.