Konrad
Konrad

Reputation: 4772

Copy complete console output in a WPF TextBox?

I' ve several places in my C# code, that output things to the Console. Some using Console.Writeline, some via TraceListeners like System.Diagnostics.ConsoleTraceListener.

Now I want to display all that output in a WPF textbox. Is it possible to bind the console output 1:1 in some way to a TextBox?

Or do I have to change every single call of TraceListeners or Console.Writeline to add the output to textbox.text additionally?

Upvotes: 0

Views: 1577

Answers (1)

Sharada
Sharada

Reputation: 13601

You can use Console.OpenStandardOutput to intercept Console.Writeline()- This answer and this link explain how to implement a TextBoxStreamWriter and inject it in standard output stream.

As for ConsoleTraceListener - you should be able to write your own textbox based TraceListener and assign it to appropriate TraceSources (as they support multiple trace listeners). This answer highlights how to implement the same.

More details regarding trace sources and listeners can be found here.

Upvotes: 2

Related Questions