Sie sind auf Seite 1von 41

Building C# Applications

Installing the .NET Framework 2.0 SDK


Before developing the .NET application using c#, the first
step is to install the freely downloadable .NET Framework 2.0
Software Development Kit (SDK).

If you have plans to use Visual C# 2005 Express, or Visual


Studio 2005, there is no need to manually download or install
.NET Framework 2.0 SDK.

8/30/2017 JNNCE 2
If you are not developing with VS 2005 or Visual C# 2005
Express, navigate to http://msdn.microsoft.com/netframework
and search for .NET Framework 2.0 SDK.
After completing the installation , development machine will be
configured with the necessary .NET infrastructure, and also now
contain numerous development tools, a very robust help system,
sample code, and tutorials, as well as various white papers.

8/30/2017 3
Subdirectories of the .NET Framework 2.0 SDK Installation Root

8/30/2017 4
The C# Command-Line Compiler (csc.exe)
There are a number of techniques you may use to compile C#
source code.
Apart from using Visual Studio 2005, you are able to create .NET
assemblies using the C# command-line compiler, csc.exe ( C-
Sharp Compiler). This tool is included with the .NET Framework
2.0 SDK.

8/30/2017 5
Configuring the C# Command-Line Compiler
C:\Windows\Microsoft.NET\Framework\v2.0.50215

8/30/2017 JNNCE-MCA 6
Configuring Additional .NET Command-Line Tools

Before we begin to investigate csc.exe, add the following additional


Path variable to the System Variables list box
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin
The above directory contains additional command-line tools that are
commonly used during .NET development.

8/30/2017 JNNCE-MCA 7
Building C# Applications Using csc.exe
Once development machine recognizes csc.exe, the next goal is to
build a simple single file assembly named TestApp.exe using the
C# command-line compiler and Notepad.
First, you need some source code. Open Notepad and enter the
following:

8/30/2017 JNNCE-MCA 8
// A simple C# application.
using System;
class TestApp
{
public static void Main()
{
Console.WriteLine("Testing! 1, 2, 3");
}
}

8/30/2017 JNNCE-MCA 9
How to Compile & Run..?
File name can be saved using the class name of the program itself
or can be saved by any name with .cs extension.
For example, the previous program could be saved by name
TestApp.cs or by Test.cs.
Save the TestApp.cs in a folder.

8/30/2017 JNNCE-MCA 10
Open the Command prompt and move the folder name.
For compiling the program type csc <filename.cs>.
Here it is csc TestApp.cs
To run the program type <filename>.
Here it is TestApp

8/30/2017 JNNCE-MCA 11
Referencing External Assemblies
Lets examine how to compile an application that makes use of
types defined in a separate .NET assembly.
using System;
using System.Windows.Forms;
class TestApp1
{
public static void Main()
{
Console.WriteLine("Testing! 1, 2, 3");
MessageBox.Show("Hello...");
}
}

8/30/2017 JNNCE-MCA 12
Given that you have made use of the MessageBox class, it is used
from the System.Windows.Forms.dll assembly.
csc /r:System.Windows.Forms.dll Testapp1.cs

8/30/2017 JNNCE-MCA 13
Output-centric Options of the C# Compiler

csc /out:<file.exe> <file.cs>

8/30/2017 14
To compile TestApp.cs into a console application named
TestApp.exe, change to the directory containing your source code
file and enter the following command set:
csc /target:exe TestApp.cs

Most of the C# compiler flags support an abbreviated version, such


as /t rather than /target
csc /t:exe TestApp.cs

8/30/2017 JNNCE-MCA 15
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
usage of /out
csc /t:exe /out:<*.exe> <*.cs >
csc /t:library /out:<*.dll> <*.cs >

8/30/2017 JNNCE-MCA 16
Compiling Multiple Source Files with csc.exe
Previously we have created a single source file having *.cs. But
sometimes most of the projects are composed of multiple *.cs files.
Assume you have created an additional class contained in a new
file named HelloMsg.cs:

8/30/2017 JNNCE-MCA 17
using System;
using System.Windows.Forms;
class HelloMessage
{
public void Speak()
{
MessageBox.Show("Hello...");
}
}

8/30/2017 JNNCE-MCA 18
using System;
class TestApp
{
public static void Main()
{
Console.WriteLine("Testing! 1, 2, 3");
HelloMessage h = new HelloMessage();
h.Speak();
}
}

8/30/2017 JNNCE-MCA 19
You can compile your C# files by listing each input file explicitly
csc /r:System.Windows.Forms.dll Testapp.cs Hellomsg.cs
OR
csc /r:System.Windows.Forms.dll *.cs

8/30/2017 JNNCE-MCA 20
Referencing Multiple External Assemblies
How to reference numerous external assemblies using csc.exe?
Simply list each assembly using a semicolon-delimited list.
csc /r:System.Windows.Forms.dll;System.Drawing.dll *.cs

8/30/2017 JNNCE-MCA 21
Working with Csc.exe Response Files
Building a complex C# application at the command prompt is full
of pain as you type in the flags that specify numerous referenced
assemblies and *.cs input files.
To help lessen the typing burden, the C# compiler honors the use
of response files.
C# response files contain all the instructions to be used during the
compilation of your current build. By convention, these files end in
a *.rsp (response) extension.

8/30/2017 JNNCE-MCA 22
Assume that you have created a response file named TestApp.rsp
that contains the following arguments.

# This is the response file for the TestApp.exe app


# External assembly references.
/r:System.Windows.Forms.dll
# output and files to compile (using wildcard syntax).
/target:exe /out:<filename>.exe <filename>.cs

Now, assuming this file is saved in the same directory as the C#


source code files to be compiled, you are able to build your entire
application as follows: csc @TestApp.rsp

If the need arise, you are also able to specify multiple *.rsp files as
input (e.g., csc @FirstFile.rsp @SecondFile.rsp @ThirdFile.rsp).
8/30/2017 JNNCE-MCA 23
The Default Response File (csc.rsp)
The C# compiler has an associated default response file (csc.rsp),
which is located in the same directory as csc.exe itself (e.g.,
C:\Windows\Microsoft.NET\Framework\v2.0.50215).
If you open this file using Notepad, you will find that numerous
.NET assemblies have already been specified using the /r: flag.
In the presence of the default response file, the current TestApp.exe
application could be successfully compiled using the following
command set (as System.Windows.Forms.dll is referenced within
csc.rsp):
csc /out:TestApp.exe *.cs

8/30/2017 JNNCE-MCA 24
Generating Bug Reports
The raw C# compiler provides a helpful flag named /bugreport.
This flag allows you to specify a file that will contain various
statistics regarding your current build; including any errors
encountered during the compilation process.
csc /bugreport:bugs.txt *.cs
When you specify /bugreport, you will be prompted to enter
corrective information for the possible errors, will be saved into the
file specified.

8/30/2017 JNNCE-MCA 25
Remaining C# Compiler Options
The C# compiler has many other flags that can be used to control
how the resulting .NET assembly is to be generated.
For ex., flags exist to build XML source code documentation files,
create multifile assemblies, and embed .NET resource files.

8/30/2017 JNNCE-MCA 26
8/30/2017 JNNCE-MCA 27
8/30/2017 JNNCE-MCA 28
8/30/2017 JNNCE-MCA 29
The Command-Line Debugger (cordbg.exe)
.NET Framework 2.0 SDK comes with a command-line debugger
named cordbg.exe.
This tool provides the options that allows to debug your assembly.

A Handful of Useful cordbg.exe Command-Line Flags


8/30/2017 JNNCE-MCA 30
Debugging at the Command Line
Before you can debug your application using cordbg.exe, the first
step is to generate debugging symbols for your current application
by specifying the /debug flag of csc.exe.
For example, to generate debugging data for TestApp.exe, enter the
following command set:
csc @testapp.rsp /debug
This generates a new file named (in this case) testapp.pdb.
Once you have generated a *.pdb file, open a session with
cordbg.exe by specifying your .NET assembly as a command-line
argument:
cordbg.exe testapp.exe

8/30/2017 JNNCE-MCA 31
Using the Visual Studio .NET IDE
&
Other Key Aspects of the VS .NET IDE
For self-reading

8/30/2017 JNNCE-MCA 32
C# "Preprocessor" Directives
C# supports the use of various symbols that allow you to interact
with the compilation process.
In C#, there is no separate preprocessing step. Rather,
preprocessing directives are processed as part of the lexical analysis
phase of the compiler.

8/30/2017 JNNCE-MCA 33
Specifying the Code Regions
#region and #endregion are most used preprocessor directives.
Using these tags, you are able to specify a block of code that may
be hidden from view and identified by a friendly textual marker.
Use of region helps to manage the lengthy *.cs files.
class ProcessMe
{
.......
//Nested types will be examined later.
#region Stuff I don't care about
public class MyHelperClass
{
//stuff....
}
public interface MyHelperInterface
{//stuff....
}
#endregion
}8/30/2017 JNNCE-MCA 34
8/30/2017 JNNCE-MCA 35
Conditional Code Compilation
#if, #elif, #else, #endif allows you to conditionally compile a block
of code based on the predefined symbols.
The use of these directives is to include additional code which are
compiled only under debug builds.
using System;
classs ProcessMe Here, you are checking
{
.... for a symbol named
static void Main(string[] args)
{
DEBUG. If it is present,
//Are we in debug mode? the lines wil be compiled.
#if (DEBUG)
Console.WriteLine("App directory: {0}", If the DEBUG symbol is
Environment.CurrentDirectory);
Console.WriteLine("Box:{0}",
not defined, the code
Environment.MachineName); placed between #if and
Console.WriteLine("OS:{0}",
Environemnt.OSVersion); #endif will not be
Console.WriteLine(".NET Version:{0}",
Environment.Version); compiled into the
}
#endif resulting assembly, and it
} 8/30/2017 JNNCE-MCA will be ignored. 36
The next logical question is where exactly is the DEBUG symbol
defined? Well, if you wish to define this symbol on a file by file
basis, you are able to make use of the #define preprocessor
symbol as follows:

#define DEBUG
using System;
classs ProcessMe
{
....
static void Main(string[] args)
{
//Are we in debug mode?
#if (DEBUG)
Console.WriteLine("App directory: {0}",
Environment.CurrentDirectory);
Console.WriteLine("Box:{0}",
Environment.MachineName);
Console.WriteLine("OS:{0}",
Environemnt.OSVersion);
Console.WriteLine(".NET Version:{0}",
Environment.Version);
#endif
}
} 8/30/2017 JNNCE-MCA 37
Issuing Warnings and Errors
#warning and #error preprocessor directives allow you to instruct
the c# compiler to generate a warning or error on the fly.
For example, assume that you wish to ensure that when a given
symbol (such as DEBUG) is defined in the current project, a
compiler warning is issued
static void Main(string[] args)
{
//Are we in debug mode?
#if (DEBUG)
#warning Beware!Debug is defined...configure release build.
//same code as before...
#define
}

On the next compile you will find your custom warning (or error,
if using #error) is displayed in the Output window.
8/30/2017 JNNCE-MCA 38
Altering Line Numbers
#line directive allows you to alter the compilers recognition of line
numbers during its recording of compilation warnings and errors.
The #line directive allows you to specify the name of a file to dump
compiler anomalies.
To reset default line numbering, you must specify the default tag.

//The warning below will be issued on line


//Set line number to 3000
#line 3000
#warning "Custom warning on line 3000(for no good reason)...
//Resume default line numbering.
#line default

8/30/2017 JNNCE-MCA 39
An Interesting Aside: The System.Environment Class
The System.Environment class allows you to obtain a number of
details regarding the context of the operating system hosting your
.NET application using various static members.

8/30/2017 JNNCE-MCA 40
using System;
class PlatformSpy
{
public static int Main(string[] args)
{
//OS version currently used
Console.WriteLine("current os: {0}",Environment.OSVersion);

//Directory?
Console.WriteLine("Current Directory: {0}",Environment.CurrentDirectory);

//Here are the drives on this box.


String[] drives = Environment.GetLogicalDrives();
for(int i=0;i<drives.Length;i++)
Console.WriteLine("Drive {0} : {1}", i, drives[i]);

//Which version of the .NET platform?


Console.WriteLine("Current Verion of .NET: {0}",Environment.Version);
return 0;
}
}

8/30/2017 JNNCE-MCA 41

Das könnte Ihnen auch gefallen