Compiling C# Code from the Command Line (Updated)

Last Saturday, I got a text from my daughter asking me how much it would cost to replace the fan on her laptop (a 4-year-old Dell Inspiron 1420). Being the cynical cheapskate that I am, I figured that the fan would probably cost more than the laptop itself. I ended up cleaning the cooling fan filter which seems to be doing the trick. But along the way, I managed to review, refresh and use the command line C# compiler that comes FOR FREE with every instance of Windows. Here’s what I did.

[Update: I have now provided a short Tutorial on how to do this yourself]

Since her laptop was getting hot, I figured that I should make some tool to monitor her CPU temperature. After some deft Googling, I came across this code on

“Perfect!” I thought. “I will just write a small app that uses this code.” But then I remembered that there are no development tools installed on her computer (Liberal Arts major that she is, she has no need for them).

“No problem! To the command line!” I said, in my best super-hero voice. After all, the C# compiler that is used by Visual Studio is installed as part of the .NET Framework. So it actually resides in the “C:Windows…” hierarchy.

Once in the sweet confines of the good-ole DOS box, I launched an instance of Notepad and copied the code from the class I found in the website, above. I then added a wrapper class which contained a “Main” function and called to the Temperature class.

Then back in the DOS box, I entered the “CSC.EXE” command and added my .CS file as a parameter and I was immediately met with some errors. I read the errors, fixed the items the errors referenced and ran the compiler once again. A second set of errors related to missing libraries. Hmm, time to add some more parameters to the command line. Recompile, and then…Viola! My .EXE was sitting next to my .CS file.

As nervous as contestant on “Who Wants to be a Millionaire”, I entered “GetTemp.EXE” into the DOS box prompt, hovered over the ENTER key, pressed it and….

… it worked! There, in front of my eyes, was a glorious “43.5” (degrees Celcius) displayed on-screen. Shouts of joy echoed throughout the realm (my living room, at least) as the cobwebs were dusted off some skills not recently used.

So, where does one go to find out about compiling from the command line? Here are some links to the MSDN documentation regarding command-line compiling using the C# compiler:
Building from the Command Line
C# Compiler Options

A much more powerful tool is MSBuild.EXE (It was installed as part of Framework v2 and v3.5 but it does not appear to be installed as part of Framework version 4; However, there is a .NET 4 SDK which seems to include MSBuild).

MSBuild makes use of the C# compiler that I mentioned above. However, MSBuild uses a project file to define build packages. These packages in turn can be used to define groups of files, compiler options, pre-compile and post-compile tasks to customize the build process. This is, pretty much, what Visual Studio does for you when you press “F6” to build your solution.

References for MSBuild are located here:
MSBuild Command Line Reference

These are two tools which many developers (most developers?) have never used, or are even aware of. But, professional developers should know that these tools exist, and more importantly, should know how to use them.

So when your daughter comes to you with a laptop that is overheating, you can write a small application that tells you that the CPU is running at “43.5” degrees.

Leave a Comment

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