Writing Console Applications in .Net - ' Redirecting Your Attention ' (
Page 2 of 4 )
A console application does not make use of the visual interface capabilities of the Windows operating system. It runs at the DOS prompt, or command window, and its interactions with the user are limited to text input and output. It sounds pretty basic, and it is.
Why would anyone want to write a console application today? Aren't they a technological dinosaur with no relevance in today's world of snazzy graphical user interfaces? No — the fact is that console applications definitely have a place in the programmer's toolbox. And, because they are supported as a project type in Visual Studio .Net, they are quite easy to create.
ADVERTISEMENT
The term console comes from the days of the mainframe, when the computer itself was hidden away in an air-conditioned room; the users sat at remote terminals consisting of a video display and a keyboard. Together, the keyboard and display were called the console. The user could type text in, and the computer could display text. This limited interaction may seem archaic, but the fact is that some programs do not need more sophisticated input and output. In fact, some need none at all — you start them, they perform a task, and then
quit. Some examples of programs that need little or no interaction with the user are:
Copy all Excel and Word documents from your documents folder to a backup location.
Go through a database to archive all data that is more than a year old.
Convert all graphics files in a folder from one file format to another.
The technique of redirection provides even more flexibility for console applications. To understand redirection, you need to know about the standard input and standard output streams. Provided by the operating system, these streams provide the means for a console application to input and output text. The standard input and output streams are by default connected to the console, so that's where a console application normally gets its input and sends its output. Using redirection, the standard input and output streams can be
connected to something other than the console.
The most useful type of redirection involves disk files — the program takes its input from a text file on disk, rather than from the keyboard, and its output goes to another disk file. Redirection can also send the output of out console application directly to the
input of another, Redirection provides for great flexibility in running console applications without user intervention. At the command prompt, you enable redirection as follows:
program > file The output of program goes to file instead of the console.
program >> file The output of program is appended to file instead of going to the console.
program < file The program takes its input from file rather than the console.
program < file1 > file2 The program takes its input from file1 and sends output to file2.
program1 | program2 The output of program1 is the input for program2.
You also have several choices as to how a console application is started. The traditional way — typing the program's name at the command prompt — certainly works. You can also start a console application by double-clicking the program name in Windows Explorer or by using the Run dialog box. Perhaps most important, a console application can be started from another program, as you'll see later in this chapter. Finally, you can schedule a console application to run using Windows Scheduler.