Saturday, September 8, 2012

Console Appplications in .NET

SUMMARY
The Microsoft .NET Framework is not just about Windows Forms and Web services. This article discusses the simplest kind of Framework-based application—the console app—along with the frequently overlooked constructs of standard input/output and the pipe. When designed carefully, console applications offer a surprisingly powerful way of solving complex programming problems. One of the more exciting aspects of this approach is that while each application in the pipe is fairly simple, the result of their interaction can be a relatively complex task. Here the author explores the ins and outs of writing these console apps.

Contents

The Command Prompt
Building Console Apps in .NET
A Design Pattern for Console Apps
Controlling Console Apps with the Process Class
Yes, But Can it Read an RSS Feed?
Conclusion 

The Microsoft® .NET Framework is more than Windows® Forms and Web services. In this article, I'll explore the simplest kind of Framework-based application—the console application—along with the frequently overlooked constructs of standard input/output and the pipe. If you are anything like me, you'll find yourself full of new ideas once you become familiar with these venerable concepts.
So, what is a console app? It's tempting to say that console applications are applications invoked from the Windows command prompt (cmd.exe), though this wouldn't be strictly true. Simply put, a console app is any program with access to three basic data streams: standard input, standard output, and standard error. As the names suggest, standard input represents data flowing into the program, standard output represents data flowing out, and standard error represents a special kind of data flowing out (namely, error messages). Along with its command-line arguments, these data streams represent the runtime context of the console app.
It's easy to spot a console application running in Windows; it's the familiar black box with gray text. This is the way Windows makes the standard data streams available to you. Whatever you type (or paste) into the black window becomes standard input to the console application and whatever text you see displayed is actually the standard output and/or standard error (it can be difficult to tell them apart sometimes). Here's an example. If you select Run from the Windows Start menu and type the following, a console window pops up:
at.exe /delete
The console window states: "This operation will delete all scheduled jobs. Do you want to continue this operation?" This is actually the standard output of the program, which is paused while it waits for your response to appear on standard input. By typing "n" and pressing Enter, you are sending at.exe some standard input and causing it to perform some actions (in this case, you are saying "no," causing the program to end). I chose this example because the at.exe /delete command waits for standard input from the user. Other commands (at.exe by itself, for instance), simply send something to standard output and exit before you can see what it was.

No comments:

Post a Comment