So pretty straightforward question. I have a c# .dll with a whole lot of Console.Writeline code and would like to be able to view that output in a forms application using this .dll. Is there a relatively easy way of binding the console output to a RichEdit (or other suitable control)? Alternatively, can I embed an actual console shell within the form? I have found a few somewhat similar questions but in most cases people wanted to be able to recieve console input, which for me is not necessary.
Thanks.
You can use Console.SetOut() to redirect the output. Here's a sample form that demonstrates the approach. Drop a RichTextBox and a button on the form.
I assume it won't be very fast but looked okay when I tried it. You could optimize by overriding more methods.
IMO, it would be better to refactor the existing code, replacing the existing
Console.WriteLine
to some central method in your code, and then repoint this method, presumably by supplying anotherTextWriter
:Or (simpler and less hacky re a static field), simply pass a
TextWriter
into your existing code and write to that?