Using VS - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Using VS arrow Page 4 - Writing Console Applications in .Net
Writing Console Applications in .Net
By Peter Aitken

Rate This Article: Add This Article To:

Writing Console Applications in .Net - ' Errors in Console Applications '
( Page 4 of 4 )

Errors in Console Applications

A console application can generate runtime errors just like any other program. These should be handled with VB.Net's error-handling statements. You can use either unstructured error-handling statements (On Error and Resume) or structured (Try, Catch, Finally). Unhandled errors have the same consequences as in any type of VB.Net program: The user sees an error message and then is given the option of debugging the program or quitting. Clearly, unhandled errors are to be avoided. Graceful error handling is an essential part of any application.

ADVERTISEMENT

If an unhandled error occurs, console applications have an additional method of dealing with the error message. This operates in in parallel with the usual VB.Net response to unhandled errors, as described in the previous paragraph. Error messages in a console application are sent to an output stream called Standard Error. By default, Standard Error is connected to the console, so error messages appear on-screen. Redirecting Standard Input does not redirect Standard Error. However, you can redirect Standard Error by calling the Console.SetError() method. Most often, error messages are redirected to file.

Redirection in Code

In addition to redirecting input and output using command line syntax, you can do so within your program by using the Console.SetIn() and Console.SetOut() methods. The syntax is as follows:

Console.SetIn(newIn As TextReader)
Console.SetOut(newOut As TextWriter)

TextReader and TextWriter are abstract classes and so cannot be implemented directly. You will use a derived class instead. For redirecting input and output to files, the technique is as follows:

  1. Create a FileStream object for the desired file. Be sure the file mode is set appropriately depending on whether you will be reading from the file or writing to it.
  2. Wrap the FileStream object in either a StreamReader object (for input) or a StreamWriter object (for output).
  3. Pass the StreamReader object to the Console.SetIn() method, and pass the StreamWriter object to the Console.SetOut() method.

The program in the following listing demonstrates how to redirect both input and output to files. It doesn't do anything particularly useful; it simply reads lines of text from the input, appends a few exclamation points, and then writes to the output. It requires that a text file named Input.txt be present in the same folder as the application. After the program runs, a file named Output.txt will also be present, containing the program's output.

Imports System
Imports System.IO
Sub Main()
    Dim buf As String
    Dim inFile As New FileStream("Input.txt", FileMode.Open)
    Dim inStream As New StreamReader(inFile)
    Dim outFile As New FileStream("Output.txt", FileMode.CreateNew)
    Dim outStream As New StreamWriter(outFile)
    Console.SetIn(inStream)
    Console.SetOut(outStream)
    buf = Console.ReadLine()
    Do While Len(buf) > 0
        buf = buf & "!!!"
        Console.WriteLine(buf)
        buf = Console.ReadLine()
    Loop
    inStream.Close()
    outStream.Close()
    End Sub

Summary

Console applications may seem old fashioned but they still have plenty of uses. They are easy to write thanks to the support provided in the .Net framework. They are definitely worth your consideration for any task that does not require interaction with the user.



 
 
>>> More Using VS Articles          >>> More By Peter Aitken
 



HD VOIP Has Arrived (with Tony Konstner)

Play Video >

All Videos >

Google and blonde jokes?

Read now >

Favorite books!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.