Compiling a C# Program from the Command Line

No one will ever actually do this, of course, but it’s nice to fantasize about when Visual Studio is being unreasonable about something. Which is frequently, am I right, fellas?

1. Download and install the latest version of the Microsoft .NET Framework. As of this writing, an installer is available at http://www.microsoft.com/net/download.aspx.

2. Locate the directory containing the C# compiler executable, which is named “csc.exe”. The directory will be named something cumbersome. For example, the version of csc.exe used in this tutorial was located in a directory whose path starts with “C:\Windows\winsxs\x86_netfx35linq-csharp”, followed by a guid, a version code with eleven digits in four groups, the word “none”, and finally another guid.

3. In any convenient location, create a new directory named “ConsoleCompileTest”.

4. In the ConsoleCompileTest directory, create a new text file named “ConsoleCompileTest.cs”, containing the following text.

using System;

namespace ConsoleCompileTest
{
    public class ConsoleCompileTest
    {
	public static void Main()
	{
            Console.WriteLine("Compiled from command line!");
            Console.ReadLine();
        }
    }
}

5. Still in the ConsoleCompileTest directory, create a new text file named “BuildEnvironmentSetup.bat”, containing the following text. Substitute the path of the directory where csc.exe is located for the bracketed text.

set compilerPath="[the directory where csc.exe is located]"
for %%* in (.) do (set programName=%%~n*)

6. Still in the ConsoleCompileTest directory, create a new text file named “ProgramBuild.bat”, containing the following text.

call BuildEnvironmentSetup.bat

%compilerPath%\csc.exe %programName%.cs

pause

7. Double-click the icon for ProgramBuild.bat. A console window will appear and csc.exe will compile ConsoleCompileTest.cs. Then press any key to dismiss the console.

8. In the ConsoleCompileTest directory, a new file called “ConsoleCompileTest.exe” should now be present.

9. Double-click the icon for ConsoleCompileTest.exe to run it. A console window should appear, and the text “Compiled from command line!” should appear at the top of it.

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s