Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Building C# Applications using Command Prompt

Posted by Ravi Varma Thumati on January 12, 2010

CSC stands for the C-sharp compiler, to illustrate how to build a .NET application IDE-free; we will build a simple executable assembly named TestApp.exe using the C# command-line compiler and Notepad. First, you need some source code. Open Notepad (using the Start ->Programs ->Accessories menu option) and enter the following trivial C# code:

// A simple C# application.

using System;

class TestApp

{

static void Main()

{

Console.WriteLine(“Testing! 1, 2, 3”);

}

}

Once you have finished, save the file in a convenient location (e.g., C:\CscExample) as TestApp.cs. Now, let’s get to know the core options of the C# compiler.

Specifying Input and Output Targets

The first point of interest is to understand how to specify the name and type of assembly to create (e.g., a console application named MyShell.exe, a code library named MathLib.dll, a Windows Forms application named Hello.exe, and so forth). Each possibility is represented by a specific flag passed into csc.exe as a command-line parameter

To compile TestApp.cs into a console application named TestApp.exe, change to the directory containing your source code file:

cd C:\CscExample

And enter the following command set (note that command-line flags must come before the name of the input files, not after):

csc /target:exe TestApp.cs

Here I did not explicitly specify an /out flag, therefore the executable will be named TestApp.exe, given that TestApp is the name of the input file. Also be aware that most of the C# compiler flags support an abbreviated version, such as /t rather than /target (you can view all abbreviations by entering csc -? at the command prompt).

csc /t:exe TestApp.cs

Furthermore, given that the /t:exe flag is the default output used by the C# compiler, you could also compile TestApp.cs simply by typing csc TestApp.cs

Referencing External Assemblies

Next, let’s examine how to compile an application that makes use of types defined in a separate .NET assembly. Speaking of which, just in case you are wondering how the C# compiler understood your reference to the System.Console type.

Let’s update the TestApp application to display a Windows Forms message box. Open your TestApp.cs file and modify it as follows:

using System;

// Add this!

using System.Windows.Forms;

class TestApp

{

static void Main()

{

Console.WriteLine(“Testing! 1, 2, 3”);

// Add this!

MessageBox.Show(“Hello…”);

}

}

Notice you are importing the System.Windows.Forms namespace via the C# using keyword (introduced in Chapter 1). Recall that when you explicitly list the namespaces used within a given *.cs file, you avoid the need to make use of fully qualified names of a type.

At the command line, you must inform csc.exe which assembly contains the namespaces you are using. Given that you have made use of the System.Windows.Forms.MessageBox class, you must specify the System.Windows.Forms.dll assembly using the /reference flag (which can be abbreviated to /r):

csc /r:System.Windows.Forms.dll TestApp.cs

Referencing Multiple External Assemblies

On a related note, what if you need to reference numerous external assemblies using csc.exe? Simply list each assembly using a semicolon-delimited list. You don’t need to specify multiple external assemblies for the current example, but some sample usage follows:

csc /r:System.Windows.Forms.dll;System.Drawing.dll *.cs

Compiling Multiple Source Files

The current incarnation of the TestApp.exe application was created using a single *.cs source code file. While it is perfectly permissible to have all of your .NET types defined in a single *.cs file, most projects are composed of multiple *.cs files to keep your code base a bit more flexible. Assume you have authored an additional class contained in a new file named HelloMsg.cs:

// The HelloMessage class

using System;

using System.Windows.Forms;

class HelloMessage

{

public void Speak()

{

MessageBox.Show(“Hello…”);

}

}

Now, update your initial TestApp class to make use of this new class type, and comment out the previous Windows Forms logic:

using System;

// Don’t need this anymore.

// using System.Windows.Forms;

class TestApp

{

static void Main()

{

Console.WriteLine(“Testing! 1, 2, 3”);

// Don’t need this anymore either.

// MessageBox.Show(“Hello…”);

// Use the HelloMessage class!

HelloMessage h = new HelloMessage();

h.Speak();

}

}

You can compile your C# files by listing each input file explicitly:

csc /r:System.Windows.Forms.dll TestApp.cs HelloMsg.cs

As an alternative, the C# compiler allows you to make use of the wildcard character (*) to inform csc.exe to include all *.cs files contained in the project directory as part of the current build:

csc /r:System.Windows.Forms.dll *.cs

When you run the program again, the output is identical. The only difference between the two applications is the fact that the current logic has been split among multiple files.

Advertisements

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

 
%d bloggers like this: