Liren Zhao said:
Thanks for your replay.
I'm a jackaroo in c#,would you give me a full code?
It is a full code ... At the bottom of this message you'll find it inside a
console application but nothing much changed. Just comment out the output
definition you don't want to use to test both cases.
And what is output in your code?
A String like i stated just above the code in the previous message ...
using System;
using System.IO;
namespace softtech.redirection
{
class redir
{
[STAThread]
static void Main(string[] args)
{
String output = @"c:\test.txt"; // output redirection to the file
//String output = ""; // normal output to stdout
StreamWriter sw = null ;
TextWriter tmp = Console.Out;
if (!output.Equals(""))
{
tmp = Console.Out;
FileStream fs = new FileStream(output,
FileMode.OpenOrCreate,FileAccess.Write);
sw = new StreamWriter(fs);
Console.SetOut(sw);
}
// do your work just as normal
// the program will write to the correct spot
Console.WriteLine("redirection test");
/* Output redirector clean up */
if (!output.Equals(""))
{
Console.SetOut(tmp);
sw.Close();
}
}
}
}