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 |
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. |
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 |
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 |
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.