(Updated 12/4/2015) 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%\CSC.EXE
at the command prompt you will see a listing that looks a lot like this one:
What does this command do?
dir is the Windows “DIRECTORY” listing command. By default, this lists files in the current directory.
/s indicates that we are looking in sub-directories as well.
%WINDIR%\CSC.EXE indicates the pattern of file names to match (files named “CSC.EXE”)
From this command 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:
Notepad.exe
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:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\CSC.EXE hello.cs
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.
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
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 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:
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”.
Then at the command line, type:
CSC @hello.rsp
The output will look like:
Summary
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
Thank you.
I’m glad you found this useful!
-tomas
thank you to you
but a command line environment is difficult for newbie
i think that we should use a visual studio ide is the best.
Yes, IDE is easier. As I mentioned this is most useful for situations where you do not have the IDE and you need to write some code without the tools.
Even though most people do not use the command line often, a professional should have knowledge of the command line tools in DOS, Unix/Linux, etc.
What about debugging the c# code in this environment?
Debugging can be difficult in this environment.
Since, I am assuming that you don’t have an IDE and you are coding in a difficult situation, the old fashioned “Console.Write(…)” or “MessageBox.Show(…)” are your best friends if really need to debug. But if you really want to do true debugging, you have some options.
First, you will want to generate debugging information. To generate this information you would need to add an option to your command line “/debug”. You can add it directly to a command line, or include it in a response file (discussed in the article, above).
Next you will need a debugger of some sort. I have not used anything other than the VS IDE recently. But I have used other debuggers in the past and I’m sure that you can find a good debugger available for download somewhere.
So bottom line: debugging will be hard, but not impossible. Let me know if this is a topic that you believe I should cover in some detail.
-tomas
Tomas, Hi my computer is saying that my visualc#compiler has stopped working is this something I need to have fixed or is this just a bonus that comes with the software????? I dont use it for business just for personel use job searching and such.
Marilyn, Unfortunately, troubleshooting an error such as this could involve reviewing logs (among other things) to try and identify the actual cause of the error. It likely not the C# compiler itself that is bad. But rather something that is trying to use the C# compiler that is having trouble.
Here is one of many possible trouble shooting scenarios. LINK
Sorry I could not be of more help.
can any one plz tell me
when i type notepad.cs in cmd it shows d message:’notepad.cs’is not recognized as an internal or external command..
You are supposed to type “notepad.exe” . Or just “notepad”.
Hi, i save the file hello.cs but when i try to compile , have a message source file hello.cs could not be found.Can you tell me why ?
You must ensure that you are either
1) in the directory to which you saved file “hello.cs”, or
2) you must provide a fully qualified path to the file “hello.cs”
The message indicates that CSC can not find file “hello.cs”
Hi, i save the file hello.cs but when i try to compile , have a message source file hello.cs could not be found.Can you tell me the path where to place hello.cs file in which folder?
Ensure that your current directory is the one from which you are typing in the command.
For example: If you save “hello.cs” as “c:classroom_assignmenthello.cc”, then you must ensure that you are in folder “c:classroom_assignment” when you execute the command.
Alternatively, you could enter:
C:WindowsMicrosoft.NETFrameworkv4.0.30319CSC.EXE c:classroom_assignmenthello.cs
from the DOS prompt in any folder and it will work (be sure to escape any filename with spaces in them).
Contact me if you still have questions.
P.S. By any chance are you at Barani?
hi! i did what you said, but unfortunately, this error appears in the command prompt now:
error CS1567: error generating Win32 resource: Access denied.
and a warning
CS1610: unable to delete temporary file.
could anyone give me some help, pretty please?
Thanks for stopping by my site!
Entering that error message into Google takes me straight to MSDN. MSDN is the best resource for compiler errors.
Here are two links that give some info on the error messages. I hope they help.
For error 1567: http://msdn.microsoft.com/en-us/library/h3a82eh8.aspx
For error 1610: http://msdn.microsoft.com/en-us/library/z4aytccf.aspx
Thank you… It’s Working… 🙂
Thank you!! You’ve made my day!
hello,
My system shows this error when i type this command C:WindowsMicrosoft.NETFrameworkv4.0.30319CSC.EXE hello.cs
Microsoft visual c# 2010 Compiler version 4.0,30319.1
Copy Right Microsoft Corporation.All rights reserved
error CS2001:source file ‘hello.cs’ could not be found
fatal error CS2008:No inputs specified
Please see my reply to a visitor named Hammad in this thread.
Thank you for visting!
thankxxxx tomas…great job!
i m done with it 😛
Finally I got one which is working…….. 🙂
Thank you, this was most usefull.
When i try to compile the hello.cs file i get the errors
hello.cs(13,13): error CS0246: The type or namespace name ‘Echo’ could not be
found (are you missing a using directive or an assembly reference?)
hello.cs(13,36): error CS0246: The type or namespace name ‘Echo’ could not be
found (are you missing a using directive or an assembly reference?)
And with the goodbye.cs file i get
error CS5001: Program ‘c:Userssbonelo.ndlelaDocumentscscgoodbye.exe’ does
not contain a static ‘Main’ method suitable for an entry point
Thank you Tomas 🙂 🙂 🙂