11

This may be completely impossible, but I was wondering if there is a way to read values that the console has already printed. For example, if the console printed

You are travelling north at a speed of 10m/s

as a result of Console.WriteLine("You are travelling north at a speed of 10m/s");, is there a way of reading this line and then, for arguments sake, putting this value in a string?

Basically what I need is to read what has already been outputted to the console, not the user input. Is there a way?

Thanks in advance.

3
  • 3
    I think the answers written so far are misunderstand the question. Sounds like OP want's to get all text which is written to console (in somehow) as a programmatically and put it a string variable. Commented Jan 21, 2016 at 9:48
  • Possible duplicate of Capturing console output from a .NET application (C#) Commented Jan 21, 2016 at 9:51
  • 1
    Maybe the second answer from this question is what you are looking for: stackoverflow.com/questions/12355378/… Commented Jan 21, 2016 at 10:00

4 Answers 4

20

Yes. There is a way. You can Console.SetOut method.

Organize your main method as;

static void Main() { using (StringWriter stringWriter = new StringWriter()) { Console.SetOut(stringWriter); //All console outputs goes here Console.WriteLine("You are travelling north at a speed of 10m/s"); string consoleOutput = stringWriter.ToString(); } } 

Then consoleOutput should have You are travelling north at a speed of 10m/s.

Sign up to request clarification or add additional context in comments.

1 Comment

But the message won't show in the actual console window.
17

If you want still want your output to hit the console, you can use Console.SetOut and give it two destinations at once, one being the main console window and the other being your internal store.

You could write a simple helper class like this:

public class OutputCapture : TextWriter, IDisposable { private TextWriter stdOutWriter; public TextWriter Captured { get; private set; } public override Encoding Encoding { get { return Encoding.ASCII; } } public OutputCapture() { this.stdOutWriter = Console.Out; Console.SetOut(this); Captured = new StringWriter(); } override public void Write(string output) { // Capture the output and also send it to StdOut Captured.Write(output); stdOutWriter.Write(output); } override public void WriteLine(string output) { // Capture the output and also send it to StdOut Captured.WriteLine(output); stdOutWriter.WriteLine(output); } } 

Then in your main code you could wrap your statements as shown below:

void Main() { // Wrap your code in this using statement... using (var outputCapture = new OutputCapture()) { Console.Write("test"); Console.Write("."); Console.WriteLine(".."); Console.Write("Second line"); // Now you can look in this exact copy of what you've been outputting. var stuff = outputCapture.Captured.ToString(); } } 

You could change this to have multiple destinations, so you could create an internal store that was something like List<string> instead if you wanted to.

Background: I did something along these lines (although I didn't keep a copy of the output) when I wanted to get my NHibernate queries to be output into the SQL Output tab in LINQPad. I wrote about it here (there's a Github repo and NuGet packages too): https://tomssl.com/2015/06/30/see-your-sql-queries-when-using-nhibernate-with-linqpad/

1 Comment

Using the OutputCapture class, nothing seems to be shown in the console when using Console.ReadKey() - any suggestions?
-4

It's generally a bad idea to read values from the console unless it is user input. You should put the value into a variable, or if you need to read it at a later date, save it to a file.

String a = "You are travelling north at a speed of 10m/s" Console.WriteLine(a); //Do anything you want with variable 'a' 

Comments

-5

If you want to save what you are writing to the console, store it in a String variable first and then print the contents of this variable to the console, like so:

String consoleOutput = "You are travelling north at a speed of 10m/s" Console.WriteLine(consoleOutput); 

That way you can access consoleOutput whenever you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.