Compiling from the Command Prompt

This tutorial shows you how you can compile a simple piece of code from the C# command prompt compiler and how to add references.

This tutorial will show you how to compile C# code from the DOS command prompt. It doesn’t analyze all the parameters you can pass to the compiler and any other compiling methods, it only shows you how to compile a simple piece of code.

First we have to create a C# file that we shall compile:

namespace Form1


   public class Form1: System.Windows.Forms.Form


      public Form1()


         this.Text = "Handmade form";


      public static void Main()


         System.Windows.Forms.Application.Run(new Form1());




Paste this into Notepad and save it at C:\toCompile\Form1.cs.

Now from the Start Menu in the Microsoft Visual Studio .NET\Visual Studio .NET Tools program group, open Visual Studio .NET 2003 Command Prompt:

The command prompt compiler starts:

Setting environment for using Microsoft Visual Studio .NET 2003 tools.

(If you have another version of Visual Studio or Visual C++ installed and wish

to use its tools from the command line, run vcvars32.bat for that version.)

C:\Documents and Settings\Andrei>

We change to the toCompile directory using the command CD C:\toCompile at the command prompt:

C:\Documents and Settings\Andrei>CD C:\toCompile


Now let’s try to compile the program using the csc command. But first, we should not forget that we need to add some references like we usually do in Visual Studio.

Type csc /? in the command prompt to list the help associated with this command. You can see that using /reference we can add references for the compilation of the code:

/reference:<file list>  Reference metadata from the specified assembly files

                (Short from: /r)

Now that we know how to add a reference we can compile.

C:\toCompile>csc Form1.cs /reference:System.dll /reference:System.Windows.Forms.dll

Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4

for Microsoft (R) .NET Framework version 1.1.4322

Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

That’s it. You can now open the compiled application by typing Form1 because the file Form1.exe is in the toCompile folder.

When you run the application you’ll see that a console window and an empty form will pop-up.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top