Saturday, March 9, 2013

How to Easily Compile C# Programs without Visual Studio

I've noticed that there are a lot of tutorials on the web about programming in C#, but there aren't very many tutorials out there that teach you how to actually compile and run the code that you write. In fact, it seems to me that every tutorial just tells you to install Visual C# Express (or VS Express for Web, or whatever) and then expects the rest to magically work itself out.

The problem with this is that anyone who opens Visual Studio for the first time and isn't already an expert developer will be immediately lost in the sea of options, buttons, and menu items that VS has to offer. At least that is what happened, and continues to happen, to me! Luckily, there is an easy, non-bloated way to compile C# programs that bypasses Visual Studio altogether. Just make sure you have the .NET Framework installed. You can download it here: http://www.microsoft.com/net

Command Line to the Rescue!

It turns out that the Microsoft.NET Framework (which is the backbone of Visual Studio) uses a simple executable called csc.exe to compile C# programs. So, if you've written a C# program and saved it with the filename "myprogram.cs", you can compile that program from the command line using csc.exe. The syntax would look something like this:

csc myprogram.cs

The output will be an executable file named myprogram.exe.

Now, if you typed the above command into your command line, you probably noticed that it didn't work and you got an error telling you something about 'csc' not being recognized as a command. This is because you'll need to specify the path to csc.exe on your computer before you can run it. On my machine, csc.exe lives in the C:\Windows\Microsoft.NET\Framework\v3.5\ directory. Thus, the command to compile myprogram.cs would be:

C:\Windows\Microsoft.NET\Framework\v3.5\csc myprogram.cs

NOTE: I'm using version 3.5 of the .NET Framework for these examples. Be sure to use the correct path for whichever version you have installed.

You can also specify an alternate location or filename for the output (.exe) file using the /out parameter. For example:

C:\Windows\Microsoft.NET\Framework\v3.5\csc /out:mycustomfilename.exe myprogram.cs

You can even use the * wildcard to compile multiple .cs files in one sweep:

C:\Windows\Microsoft.NET\Framework\v3.5\csc *.cs

Environment Variables make it Easier

If you're going to be developing C# applications, you're going to be compiling and running your programs very frequently and you're probably going to quickly tire of typing the full path to csc.exe each time. Not to fear! You can set up a system environment variable to make csc.exe accessible from any directory

NOTE: You will need Administrator privileges for the following steps.

To set up a system environment variable in Windows 7:
  1. Click on "Start" and right-click on "Computer"
  2. Click "Properties"
  3. Click the "Advanced system settings" link (on the left)
  4. Click the "Environment Variables..." button (at the bottom)
  5. Under the "System variables" section single-click (highlight) the "Path" item.
  6. Click "Edit..."
  7. Append the text

        ;C:\Windows\Microsoft.NET\Framework\v3.5\

    to the "Variable value" field. Note the semicolon (;) and be very careful not to erase any of the existing system variables. All you're doing is adding another variable to the end of the list.
  8. Click "OK" to save your edit.
Now you no longer have to specify the path to csc.exe when running it from the command line. Simply typing "csc" will do the trick.

Other Options

Sometimes when compiling, you will see various messages appear in the command line (error messages, warnings, etc.). If you need to save these messages for any reason, you can tell csc.exe to dump them into a .txt file by using the > operator. For example:

csc myprogram.cs > filename.txt

Also, if you're not a command line type of a person, you could always create a batch file to handle the command line stuff for you by typing the necessary compilation commands (see above) into a .txt file and saving it with a .bat extension. The resulting file (filename.bat) should be placed in the same folder as the .cs files that you with to compile. When double-clicked, the batch file will execute the commands it contains exactly as if they had been typed in the command line. More about batch files here: http://www.robvanderwoude.com/batchfiles.php

Conclusion

You should now know how to compile and run C# programs without relying on Visual Studio. I hope this post has been helpful. Now, start programming.