469,917 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

How to get Conole output information in my winform program?

I use some "Console.WriteLine(some strings here)" to
display some
debug information in my winform program.
How can I get the information in my programe or same them
in a text file ?

Nov 15 '05 #1
3 3534

"Liren Zhao" <li*******@tom.com> schreef in bericht
news:00****************************@phx.gbl...
I use some "Console.WriteLine(some strings here)" to
display some
debug information in my winform program.
How can I get the information in my programe or same them
in a text file ?


I'm not saying the following is a clean implementation but it works. The
code depends on the value of the String output. If output is empty all data
is written to the console otherwise the console is redirected to the file
specified by output :

/*******************************************/
/* Output redirector */
StreamWriter sw = null ;
TextWriter tmp = Console.Out;
if (output != "")
{
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
Console.WriteLine("redirection test");

/* Output redirector clean up */
if (output != "")
{
Console.SetOut(tmp);
sw.Close();
}
/*******************************************/

Yves
Nov 15 '05 #2
Thanks for your replay.
I'm a jackaroo in c#,would you give me a full code?

And what is output in your code?

"phoenix" <pa******@skynetWORK.be> 写入消息
news:#C**************@TK2MSFTNGP11.phx.gbl...

"Liren Zhao" <li*******@tom.com> schreef in bericht
news:00****************************@phx.gbl...
I use some "Console.WriteLine(some strings here)" to
display some
debug information in my winform program.
How can I get the information in my programe or same them
in a text file ?

I'm not saying the following is a clean implementation but it works. The
code depends on the value of the String output. If output is empty all

data is written to the console otherwise the console is redirected to the file
specified by output :

/*******************************************/
/* Output redirector */
StreamWriter sw = null ;
TextWriter tmp = Console.Out;
if (output != "")
{
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
Console.WriteLine("redirection test");

/* Output redirector clean up */
if (output != "")
{
Console.SetOut(tmp);
sw.Close();
}
/*******************************************/

Yves

Nov 15 '05 #3
"Liren Zhao" <li*******@tom.com> schreef in bericht
news:eL**************@TK2MSFTNGP12.phx.gbl...
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 ...
"phoenix" <pa******@skynetWORK.be> 写入消息
news:#C**************@TK2MSFTNGP11.phx.gbl...

"Liren Zhao" <li*******@tom.com> schreef in bericht
news:00****************************@phx.gbl...
I use some "Console.WriteLine(some strings here)" to
display some
debug information in my winform program.
How can I get the information in my programe or same them
in a text file ?


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();
}
}
}
}
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by jdionne | last post: by
3 posts views Thread by Scott Meddows | last post: by
4 posts views Thread by Kevin Mansel via .NET 247 | last post: by
2 posts views Thread by Steve | last post: by
reply views Thread by Tom | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.