Software Development: Take command of your .NET code

In this time of graphical IDEs, some developers prefer to use the versatility of the command line environment. The .NET Framework SDK even has a command line environment ready for download. Take a look at what it can do for you in this exploration.

Developers have always had the option of using command line tools for working with Java code by way of the Sun Java Development Kit. This option is now available to Windows developers with the .NET Framework. It provides an alternative to applications like Visual Studio .NET and Borland’s C# Builder by providing necessary tools at your fingertips in a DOS prompt. The tools are easy to use, but the environment must be properly configured.

Environment setup
The command line tools are installed with the Microsoft .NET Framework SDK. The SDK is freely available for download, so you can get started with .NET development without spending money on commercial tools. The default installation directory for the .NET 1.1 SDK is:

This is the base directory, and it contains the executables for using the command line tools. This directory should be placed in the computer’s system path, so it is readily available from any command prompt. The system path tells the system where to look to execute commands entered at a command prompt. This may be set via the control panel’s system option on a Windows machine. In Windows XP, the system path is edited via the environment variables button on the advanced tab with Figure A showing the results of selecting this button.

Figure A
Setting system path in Windows XP

With this setup, the .NET command line tools are available from any command prompt (DOS Window). A directory listing of the .NET Framework installation directory reveals the available tools. Table A provides a sampling of the tools.
Table A
Command Description
ai.exe Assembly Linker is used to create assembly manifests and to install assemblies into the global assembly cache (GAC).
caspol.exe The Code Access Security Policy tool allows you to modify the security policy for the machine, user, and enterprise policy level.
csc.exe C# compiler
installutil.exe Install server resources like a Windows Service
jsc.exe Jscript compiler
ngen.exe The Native Image Generator creates a native image from a managed assembly and installs it into the native image cache on the local computer.
regasm.exe The Assembly Registration tool reads the metadata within an assembly and adds the necessary entries to the registry, allowing COM clients to create .NET Framework classes transparently.
vbc.exe Visual Basic .NET compiler
vjc.exe Visual J# .NET compiler
Command line .NET tool overview

Command line compilation
The tools listed in Table A allow you to work with every aspect of .NET development, but compilation will be the focus of this article. Command line compilation is a simple process. The following C# code will be used as an example:
public class BuilderTest {
public static void Main(string[] args) {
System.Console.WriteLine(" Rules!");
} }

I created this sample with Windows Notepad, but you may choose your favorite text editor. The following command compiles the program into an executable:

The command spits out a few lines detailing the compiler and framework versions:
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

The result of compiling the small program is BuilderTest.exe that may be executed from the command line. It results in a command line program since it contains the Main method. On the other hand, a message is displayed if any errors are located and the executable is not created. I created a small error in the code listing (removing e from the call to WriteLine) and the following message was produced:,5): error CS0117: 'System.Console' does not contain
a definition for 'WriteLin'

It identifies the line number and column (5,5) where the error occurred. The VB.NET approach is the same, but of course the code is different. The following sample VB.NET will be used to test compilation:
Module BuilderVBTest
Sub Main()
System.Console.WriteLine("Hello, VB.NET World!")
End Sub
End Module

This code is saved in a file called BuilderVBTest.vbc, and the following command is used to compile it from the command line:
vbc BuilderVBTest.vbc

This produces the following output from the VB.NET compiler:
Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322.573
Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.

Again, the result of the compilation is an executable (the code has a Main sub) that may be executed from the command line. Command line compilation is good, but the compilers do offer various options for additional functionality.

Beyond basic compilation
As with most command line tools, the compilers provide various switches that may be used to control compilation as well as provide other options. While the VB.NET and C# compilers do not provide the exact same list of options, they do provide a number of overlapping features including these:
  • /debug—generate debug information
  • /define—define symbols for conditional compilation (#define)
  • /help—display list of valid command line switches (same as /?)
  • /nologo—suppress the copyright information (displayed in previous samples)
  • /optimize—turn code optimization on or off
  • /out—specify a target file for the compile results
  • /target—specifies the format of the output file using one of four options: /target:exe, /target:library, /target:module, or /target:winexe

This is a small sampling of the options, but it does provide a peek at the power available. For example, the following command compiles the VB.NET sample suppressing logo information, optimizing the code, and creating the output in the builder.exe.
vbc BuilderVBTest.vbc /out:builder.exe /nologo /optimize

Another development option
I know many hardcore Java and UNIX developers that love the control provided by a command line interface. Thankfully, the .NET Framework provides this alternative with its various tools. Most notable is the command line compiler that allows you to quickly get your code running.

Editor's Picks