Compiling a C# (C-Sharp) file using command line tools is not as difficult as you may think. In this tutorial, I will walk you through the steps needed to create a project using nothing more than Notepad and the Command Prompt. Jump to the code
A lot of programmers are not aware that the .NET compilers used by Visual Studio are installed as part of the .NET Framework itself. This allows you to use the C# and Visual Basic compilers outside of Visual Studio.
“Why would you ever want to do this?” There may come a time when you need to craft a program but you are not at a computer with Visual Studio installed. Fear not, because your command line compiler is just a few keystrokes away courtesy of our friends at Microsoft.
The command line compiler is a very powerful tool. You can build large complex Solutions from the command line just as easily as you can build a small “Hello World” program once you understand the basics concepts of compiling on the command line. So… let’s get started.
First, open a Command Prompt window. to do this, you can click the Start button, select “Run” then enter “cmd” and click the “OK” button (the Command Prompt window can also be launched from the “Start” menu: Start | “All Programs” | Accessories| “Command Prompt”).
For old-timers (such as myself) this is a bit like going back home. But for younger folks (those under 30) the command line can seem like something foreign. So I will use a bunch of screen captures to illustrate things as we go.
Step 1: let’s create folder for us to work in. When I run the Command Prompt, on my system it starts in the c:\Windows\System32 folder. This is NOT GOOD. So let’s create a safe place to work.
At the command prompt enter the following commands (each followed by <ENTER>):
cd %HOMEPATH% (Takes you to your Home folder)
mkdir ccdemo (Creates a “ccdemo” folder in your Home folder)
cd ccdemo (moves you into the folder we just created)
If all worked as expected your prompt should read something like “c:\Users\<logon>\ccdemo>” (or the Windows XP equivalent). This is a “safe” folder. We are now ready to work.
Step 2: let’s find our compilers. We will use a search to locate all the available C# compilers (there may be more than one installed on your system). The compilers will be installed under the Windows folder which is represented by the “%WINDIR%” environment variable. Specifically, inside a folder named “Microsoft.NET”. So if you enter:
dir /s %WINDIR%\Microsoft.NET\CSC.EXE
Listing of C# compilers
at the command prompt you will see a listing that looks a lot like this one:
Here you can see that there are, in fact, three compilers available. We will use the .NET version 4 compiler for the rest of this article. Make note of the folder/path to the compiler of your choice. In my case it is: C:\Windows\Microsoft.NET\Framework\v4.0.30319\CSC.EXE . We will need this!
Step 3: let’s create a source file for us to compile. To do this we will use Notepad (installed on all Windows machines). From the command prompt enter the command:
Source code used for the Hello program
You should see the familiar Notepad window open up. Enter the following code and save it under the name hello.cs . (Here is a snapshot of the code in my text editor):
Pretty simple! Notice that this “Hello World” program will print out the number of arguments specified on it’s own command line when called.
Step 4: Compile the source into an executable.
Enter the command:
This will compile your source file and you will see a “hello.exe” in the folder. Next, run your newly compiled EXE and pass in some parameters.
Output from running the Hello.EXE
The output will look something like this:
In this listing, you will also see how I called the program with some arguments so that you can see the argument count being displayed.
Step 5: OK. Now let us suppose that we have more than one class in our project. Let us also suppose that these classes are split over two (or more) .CS source files. Now what?
The command line arguments to CSC.EXE allow you to specify more than one source file. Simply specify all the source files you want to compile.
Let’s modify our “hello.cs” file to call out to a second class “Goodbye” in a source file named “goodbye.cs”. Here are two source listings for a modified “hello.cs” and the source for a new file named “goodbye.cs”. Use Notepad to edit and save “hello.cs”. Then use Notepad to create “Goodbye.cs”. Notice that with this code, not only will see the count of arguments, but we will also see the arguments themselves, displayed.
(Hello2/Goodbye2 source Images HERE)
Next, specify both files on the command line, such as:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\CSC.EXE hello.cs goodbye.cs
Updated Hello.cs source file
Source for the class to be called by the Updated Hello.cs
Here’s a screen shot of this compilation along with some runs of the executable:
As you can see, typing out the CSC command each time, along with a growing list of files can get to be a problem. So let’s start trimming things down a bit.
Step 6: Let’s simplify things. The first step in simplifying things is to call the CSC.EXE using a batch file (you could also use a command file, but I started using batch files, so that is what I will use here).
Open Notepad and enter the following code. Save the file as “csc.bat”.
A batch file allows you to specify a much shorter command line
A batch file is simply that: a “batch” of commands that will be executed in order. I won’t go into much detail on the contents of this file. If you are interested in this batch file, more information is provided here.
Once you have this file, you can simply type “CSC” to launch the compiler. Much easier!
So to compile the project, you would simply type
CSC hello.cs goodbye.cs
Using this batch file the output will look something like this:
Using a batch file to launch the command line compiler makes for a very short command line.
While we are simplifying things, let’s discuss Response files.
Step 7. Response files are used to set compiler options and directives, as well as to specify a set of source files to compile. The batch file that we used above can take up to 8 parameters. The command line compiler can take more parameters than this, and in some cases you may need to specify more many compiler options for your project. To handle this, you can create a Response file. A response file is parsed by the compiler and executes/implements options in the response file when it is compiling your project.
To create a response file, open Notepad and enter the following four lines. Save the file as “hello.rsp”.
A response file allows you to specify a single parameter in your command line compile.
Then at the command line, type:
The output will look like:
A response file makes compiling easier
So there you have it. You have just created a framework for allowing you to compile a set of source files using tools available on any Windows computer. The best part: these tools are available for FREE (which is always a good thing).
As always, if you have any questions, feel free to comment or Contact Me.
References/ Additional Reading
Here are some references that discuss all the options for the following:
Command-line Building With csc.exe(MSDN)
C# Compiler Options Listed by Category (MSDN)
@ (Specify Response File) (MSDN, but not a good resource. I may need to write a tutorial on this)
All the source files for this project: csc