By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

How to get Conole output information in my winform program?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

"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

P: n/a
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

P: n/a
"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.