Redirecting the Standard Streams in C#

Maker QR in C# Redirecting the Standard Streams

Redirecting the Standard Streams
Drawing QR Code 2d Barcode In C#
Using Barcode creation for .NET Control to generate, create Quick Response Code image in .NET framework applications.
Read QR Code In C#
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
As mentioned earlier, the standard streams, such as ConsoleIn, can be redirected By far, the most common redirection is to a file When a standard stream is redirected, input and/or output is automatically directed to the new stream, bypassing the default devices By redirecting the standard streams, your program can read commands from a disk file, create log files, or even read input from a network connection Redirection of the standard streams can be accomplished in two ways First, when you execute a program on the command line, you can use the < and > operators to redirect ConsoleIn and/or ConsoleOut, respectively For example, given this program:
Making Bar Code In Visual C#
Using Barcode maker for .NET Control to generate, create bar code image in .NET framework applications.
Scanning Bar Code In C#.NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
using System;
QR Code JIS X 0510 Creator In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
Drawing QR-Code In .NET Framework
Using Barcode generation for .NET framework Control to generate, create QR Code image in VS .NET applications.
Part I:
Making QR Code JIS X 0510 In VB.NET
Using Barcode maker for VS .NET Control to generate, create QR-Code image in VS .NET applications.
Print Bar Code In C#.NET
Using Barcode creator for VS .NET Control to generate, create bar code image in .NET applications.
The C# Language
European Article Number 13 Creator In Visual C#
Using Barcode creation for .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications.
Making EAN / UCC - 14 In Visual C#
Using Barcode creator for .NET framework Control to generate, create EAN / UCC - 14 image in VS .NET applications.
class Test { static void Main() { ConsoleWriteLine("This is a test"); } }
Data Matrix ECC200 Creation In C#
Using Barcode maker for .NET Control to generate, create Data Matrix image in .NET applications.
ISSN - 10 Creator In C#
Using Barcode drawer for .NET framework Control to generate, create ISSN image in VS .NET applications.
executing the program like this Test > log will cause the line This is a test to be written to a file called log Input can be redirected in the same way The thing to remember when input is redirected is that you must make sure that what you specify as an input source contains sufficient input to satisfy the demands of the program If it doesn t, the program will hang The < and > command-line redirection operators are not part of C#, but are provided by the operating system Thus, if your environment supports I/O redirection (as is the case with Windows), you can redirect standard input and standard output without making any changes to your program However, there is a second way that you can redirect the standard streams that is under program control To do so, you will use the SetIn( ), SetOut( ), and SetError( ) methods, shown here, which are members of Console: static void SetIn(TextReader input) static void SetOut(TextWriter output) static void SetError(TextWriter output) Thus, to redirect input, call SetIn( ), specifying the desired stream You can use any input stream as long as it is derived from TextReader To redirect output, call SetOut ( ), specifying the desired output stream, which must be derived from TextWriter For example, to redirect output to a file, specify a FileStream that is wrapped in a StreamWriter The following program shows an example:
Bar Code Decoder In .NET Framework
Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
Making EAN-13 Supplement 5 In .NET
Using Barcode encoder for VS .NET Control to generate, create European Article Number 13 image in VS .NET applications.
// Redirect ConsoleOut using System; using SystemIO; class Redirect { static void Main() { StreamWriter log_out; try { log_out = new StreamWriter("logfiletxt"); } catch(IOException exc) { ConsoleWriteLine("Error Opening Log File"); ConsoleWriteLine(excMessage); return ; } // Redirect standard out to logfiletxt ConsoleSetOut(log_out);
Read Barcode In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
Recognizing Code 128 Code Set C In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
14:
Draw Code 39 In None
Using Barcode drawer for Online Control to generate, create USS Code 39 image in Online applications.
EAN / UCC - 14 Encoder In None
Using Barcode creator for Word Control to generate, create UCC-128 image in Word applications.
Using I/O
Drawing EAN13 In Objective-C
Using Barcode creator for iPhone Control to generate, create GTIN - 13 image in iPhone applications.
Create Barcode In None
Using Barcode generator for Software Control to generate, create bar code image in Software applications.
try { ConsoleWriteLine("This is the start of the log file");
PART I PART I PART I
for(int i=0; i<10; i++) ConsoleWriteLine(i); ConsoleWriteLine("This is the end of the log file"); } catch(IOException exc) { ConsoleWriteLine("Error Writing Log File"); ConsoleWriteLine(excMessage); } log_outClose(); } }
When you run this program, you won t see any of the output on the screen, but the file logfiletxt will contain the following:
This is the start of the log file 0 1 2 3 4 5 6 7 8 9 This is the end of the log file
On your own, you might want to experiment with redirecting the other built-in streams
Reading and Writing Binary Data
So far, we have just been reading and writing bytes or characters, but it is possible indeed, common to read and write other types of data For example, you might want to create a file that contains the ints, doubles, or shorts To read and write binary values of the C# built-in types, you will use BinaryReader and BinaryWriter When using these streams, it is important to understand that this data is read and written using its internal, binary format, not its human-readable text form
BinaryWriter
A BinaryWriter is a wrapper around a byte stream that manages the writing of binary data Its most commonly used constructor is shown here: BinaryWriter(Stream outputStream)
Part I:
The C# Language
Here, outputStream is the stream to which data is written To write output to a file, you can use the object created by FileStream for this parameter If outputStream is null, then an ArgumentNullException is thrown If outputStream has not been opened for writing, ArgumentException is thrown BinaryWriter defines methods that can write all of C# s built-in types Several are shown in Table 14-5 Notice that a string is written using its internal format, which includes a length specifier BinaryWriter also defines the standard Close( ) and Flush( ) methods, which work as described earlier
Copyright © OnBarcode.com . All rights reserved.