CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2008
    Posts
    61

    Redirect Console output to RichTextBox

    I have a console application and want its output to be redirected to a RichtextBox on a form (using C# and Visual Studio 2008).

    The console application uses several Console.WriteLine() and Console.Write() overloads.

    How can I redirect the output?

  2. #2
    Join Date
    Nov 2007
    Posts
    110

    Re: Redirect Console output to RichTextBox

    So, you have two applications, one being a console app and the other a windows form app and you want to send the console app's output to a rich text box in the windows app?

    I don't know if you can "redirect it" to the windows app, necessarily, but you could use sockets or remoting to send it to the windows app.

  3. #3
    Join Date
    Jan 2008
    Posts
    61

    Re: Redirect Console output to RichTextBox

    yes i have 2 applications but I could use the second by 'calling it' (instead of 'Program' and 'Main' I could make it a class and instantiate it). But changing all Console.WriteLine() s will just be too much , so if I could could code something like Console.Out(MyRickTextBox) it would be great.

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Redirect Console output to RichTextBox

    Probably the eassiest wat to handle the is to Launch your Forms application, and have it spawn your console program as a process. When you create the process, you are allowed to specify pipes/streams for input, output (and possibly error, though that may be merged with output by the time it gets to the managed layer).

    Then you just keep a thread running which is pulling data from the "output" stream, invoke it back to your main thread, and put it in the control.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jan 2008
    Posts
    61

    Re: Redirect Console output to RichTextBox

    In the menatime i found something that works allthough I don't know what problems it can cause.
    One problem i encountered is 'if Console.ReadLine() is being used the app crashes' but i don't know if that's because of the redirection or it is 'normal' while running a non-console application.

    Another thing is I (probably) have to overload all the WriteLine() and Write() methods, something i don't like

    but here is the code :

    Code:
    	public class StringRedir : StringWriter
    	{ // Redirecting Console output to RichtextBox
    		private RichTextBox outBox;
    
    		public StringRedir(ref RichTextBox textBox)
    		{
    			outBox = textBox;
    		}
    
    		public override void WriteLine(string x)
    		{
    			outBox.Text += x + "\n";
    			outBox.Refresh();
    		}
    	}
    
    	public partial class FormResults : Form
    	{
    		private StringRedir RedirConsole;
    		private TextWriter ConsoleWriter;
    
    		public FormResults()
    		{
    			InitializeComponent();
    
    			// Here we redirect Console.WriteLine to a RichTextBox control. 
    			ConsoleWriter = Console.Out;	// Save the current console TextWriter. 
    			RedirConsole = new StringRedir(ref rtbOutput);
    			Console.SetOut(RedirConsole);	// Set console output to the StringRedir class. 
    		}
    
    		~FormResults()
    		{
    			Console.SetOut(ConsoleWriter);	// Redirect Console back to original TextWriter. 
    			RedirConsole.Close();			// Close our StringRedir TextWriter. 
    		}
    
    // + rest of usual code of a form
           }
    I don't know if I should close redirection in the destructor or maybe on another occasion (maybe while Closing??? but what Event EXACTLY?)

    But for now it seems to work at mine
    Last edited by SoftwareTester; February 2nd, 2008 at 04:02 AM.

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Redirect Console output to RichTextBox

    yep thats pretty much how you do it, though I'd recommend popping the console.out into another thread where you readline it until the thread aborts, and from there call out of the thread with the text read using a delegate, and update the text sent to the textbox from your delegate handler.

    I've done this on quite a few occasions (one for a GPG wrapper I wrote, another for console based tracing inside an old windows app (done prior to log4net) and it works well.

  7. #7
    Join Date
    Jan 2008
    Posts
    61

    Re: Redirect Console output to RichTextBox

    Have you been overloading all the WriteLine() and Write() alternatives?

    Would be much more nice if that will not be needed as that would mean that IF new overloaded methods become available those would NOT break the code.

    Your approach sounds better but a bit complicated to me (due to my poor knowledge of delegates yet) I will have a look at it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured