Building C# Applications: Unit - 2

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

1

UNIT 2

Building C# Applications

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

The Role of C# Command Line


Compiler(csc.exe)
2

You are able to create .NET assemblies using the C#

command-line compiler, csc.exe.


This tool is included with the .NET Framework 2.0 SDK.
Using csc.exe is very dicult to build a large-scale application

It is important to understand the basics of how to compile *.cs

les.
Configuring the C# Command-Line Compiler
Configuring Additional .NET Command-Line Tools

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Configuring the C# Command-Line Compiler


3

If our machine is not congured correctly, we have to specify

the full path to the directory containing csc.exe.


In order to set the full of csc.exe, follow the following steps :
i. Right-click the My Computer icon and select Properties
from the pop-up menu.
ii. Select the Advanced tab and click the Environment
Variables button.
iii. Double-click the Path variable from the System Variables
list box.
iv. Add the following line to the end of the current Path value
and end with semicolon(;):
C:/Windows/Microsoft.NET/Framework/v2.0.50215
By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Cont..
4

Once you have updated the Path variable, you may take a test

run by opening a new command window and entering [close


any open command windows ]
csc /?
If you set things up correctly, you should see a list of options
supported by the C# compiler.

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Conguring Additional .NET Command-Line


Tools
5

The other command line tools used during .NET development

are located in the following directory

C:/Program Files/Microsoft Visual Studio8/SDK/v2.0/Bin


With these two paths established, we should now be able to

run any .NET utility from any command window.


To confirm this new setting, enter the following command to

view the options of the GAC[Global Assembly Cache tool]


utility in new command prompt:

gacutil /?

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Building C# Applications Using


csc.exe
6
Once the path of csc.exe is set, the next goal is to write a simple
C# application. Open any text editor and enter the following:
Example: using System;
class TestApp {
public static void Main(){
Console.WriteLine(Testing! 1, 2, 3);
}
}
Save the above program in a convenient location as
TestApp.cs.

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Dierent options of C# compiler


Options
/out

Purpose
used to specify the name of the assembly to be
created.
By default, the assembly name is the same as
the name of the *.cs le(case of an *.dll) or the
name of the type containing the programs
Main() method (case an *.exe).
builds an executable console application.
/target:exe
This is the default le output type, and thus
may be omitted when building this application
type.
/target:library builds a single-le *.dll assembly.
builds a module. Modules are elements of
/target:module
multile assemblies
/target:winexe
free to build Windows-based applications
By Prof. Suma M G, Dept. of MCA
RNSIT Bengaluru
7

Cont..
8

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
Also be aware that most of the C# compiler flags support an

abbreviated version, such as /t rather than /target


csc /t:exe TestApp.cs
TestApp.exe can now be run from the command line.

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Referencing External Assemblies


9

If our C# application refers to the external assemblies,


Example: using System;
using System.Windows.Forms;
class ExternalAssemblyDemo{
public static void Main(string[] args) {
MessageBox.Show(Hello...);
}
}

then our application should be compiled with csc.exe by


specifying the /r:(/reference flag) option.

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

Then it appears:
By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Compiling Multiple Source Files with csc.exe


10

Most projects are composed of multiple *.cs files to keep your


code base a bit more flexible.
Example:
// The HelloMessage class
(HelloMessage.cs)

Now, update your initial TestApp class to make


use of this new type,

using System;

using System;

using System.Windows.Forms;

class TestApp{
public static void Main(){
Console.WriteLine("Testing! 1, 2,3");

class HelloMessage {
public void Speak(){
MessageBox.Show("Hello...");

HelloMessage h = new HelloMessage();

h.Speak();

}
}
By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Cont
11

You can compile your C# files by listing each input file explicitly:
csc /r:System.Windows.Forms.dll Testapp.cs Hellomessage.cs

As an alternative, the C# compiler allows wildcard character (*)


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.

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Referencing Multiple External Assemblies


12

If you need to reference numerous external assemblies using


c sc.exe, then we have to simply list each assembly using a
semicolon-delimited(;) list.
For example:

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

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Working with csc.exe Response Files


13

If you build a complex C# application at the command prompt,


it is difficult to specify numerous referenced assemblies and *.cs
input files.
To reduces this burden, the C# compiler provide the use of
response files.
Write this in the
notepad
Then what is Response
files? and save it as a
TestApp.rsp
C# response files contain all the instructions to be used
during the compilation of your current build(*.rsp).
Example: # External assembly references.
/r:System.Windows.Forms.dll
# o/p and files to compile(using wildcard syntax)

/target:exe /out:TestApp.exe *.cs


By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Cont..
14

Then, you are able to build your entire application as follows :


csc @TestApp.rsp
Response file can override with the assembly file, if you enter
the below command:
csc /out:MyCoolApp.exe @TestApp.rsp
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
Example:
C:\Windows\Microsoft.NET\Framework\v2.0.50215)
By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Cont..
15

Default response file contains the numerous .NET assemblies


have specified using the /r: flag.
So that the TestApp.exe application could be successfully

compiled using the following command


csc /out:TestApp.exe *.cs
If you wish to disable the automatic reading of csc.rsp, you

can specify the /noconfig option as follow:

csc @TestApp.rsp /noconfig

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Generating Bug Reports


16

/bugreport flag will allows you to specify the file that will
generate that have various statistics of current build including
any errors encountered during the compilation time.
Example: using System;
class BugDemo {
public static void Main() {
Console.WriteLine("Test! 1 2 3")
//Compilation time error
}
}
Then, compile the program as
csc /bugreport:bugdemo.txt BugDemo.cs
By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

The Command-Line Debugger


(cordbg.exe)
17

Cordbg.exe is a tool that provides many options that allow us


to run our .NET assemblies under debug mode.
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin
We can view all these options by typing,
cordbg /?

Or

cordbg /?

Table: cordbg.exe Command-Line Flags

Flags
Meaning
b[reak] set or display current break points
del[ete] Removes one or more break points
ex[i]
Exit the debugger
By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Cont.
18

Flags
g[o]
o[ut]
si
so
p[rint]

Meaning
Continue the process of debugging until the next
breakpoint
Step out of the current function
Step into the next line
Step over the next line
Print all loaded variables.

compile the TestApp.cs as following below:


csc TestApp.cs /debug

if there is no any compilation errors, the csc.exe tool will


generates the TestApp.pdb, TestApp.exe les .
By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Cont
19

Once you have a valid *.pdb le, we can open a session as


follows below:

cordbg.exe TestApp.exe Or cordbg TestApp.exe


Note: the *.pdb le will be loaded automatically

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Pre-Processor Directives
20

A preprocessor directive must be the only instruction on a

line.
Preprocessing directives start with #.
Whitespace is allowed before and after the #.
The # is followed by an identier that is the directive name.
Syntax:
# directive name or # Identifier
Directive Name

Meaning

#if

begin a conditional directive, testing a symbol


along with the #else, #elif, #endif, #dene, and
#undef directives

#else, #elif,
#endif

create a compound conditional directive

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Cont..
21

Directive Names

Meaning

#dene

dene a symbol, using the symbol the expression


passed to the #if directive

#undef

Undene a symbol, using the symbol the expression


passed to the #if directive

#warning

generate warning from a specic location in your


code.

#error

generate an error from a specic location in your code

#line

modify the compilers line number

#region

specify a block of code that we can expand or collapse

#endregion

block must be terminated

two methods to dene directive:


Dene in your C# program.
Dene those at command line on compile time.
By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Example
22

ExPreD.cs
Output:

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

System Environment Class


23

System.Environment class provides information about, and


means to manipulate, the current environment and platform.

This class cannot be inherited. one of the static members of this


class are as follows:
Program.cs
Environment.OSVersion - Gets the version of the operating system
Environment.Version - returns the .NET version running the application
Environment.MachineName - Gets name of the current machine
Environment.Newline - Gets the newline symbol for the environment
Environment.SystemDirectory - returns complete path to the System
Directory
Environment.UserName - returns name of the entity that invoked the
application
Environment.HasShutdownStarted -True if system is going to shut down

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Output
24

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

Questions
25

1.
2.
3.

4.
5.

Explain the command line compiler with output


options available with C# compiler.
Explain any three C# preprocessor directives.
Explain how to compile multiple source file, that
references some external assembly.
Explain how would you work with csc.exe response
files.
Write a C# program to illustrate the usage of system
environment class.

By Prof. Suma M G, Dept. of MCA

RNSIT Bengaluru

You might also like