
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
1102
|
Chapter 20: Toolbox
{
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine("Back to standard output!");
}
The console output from this code looks like this:
Stealing standard output!
Back to standard output!
log.txt contains the following after the code is executed:
Writing to the console... NOT!
0
1
2
3
4
5
6
7
8
9
Discussion
Redirecting the standard output stream inside of the program may seem a bit anti-
quated. But consider the situation when you’re using another class that writes infor-
mation to this stream. You don’t want the output to appear in your application, but
you have to use the class. This could also be useful if you create a small launcher
application to capture output from a console application.
See Also
See the “Console.SetOut Method,” “Console.OpenStandardOutput Method,” and
“StreamWriter Class” topics in the MSDN documentation.
20.9 Running Code in Its Own AppDomain
Problem
You want to run code isolated from the main part of your application.
Solution
Create a separate AppDomain to run the code using the AppDomain.CreateDomain
method. CreateDomain allows the application to control many aspects of the
AppDomain being created like the security environment, the AppDomain settings, and
base paths for the
AppDomain. To demonstrate this, the code creates ...