9

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.

Danny Goodall
  • 798
  • 1
  • 9
  • 33
  • 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. – Soner Gönül Jan 21 '16 at 09:48
  • Possible duplicate of [Capturing console output from a .NET application (C#)](http://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c) – Michael Jan 21 '16 at 09:51
  • 1
    Maybe the second answer from this question is what you are looking for: http://stackoverflow.com/questions/12355378/read-from-location-on-console-c-sharp#answer-12366307 – JTK Jan 21 '16 at 10:00

4 Answers4

19

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.

Irshad
  • 3,071
  • 5
  • 30
  • 51
14

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/

Community
  • 1
  • 1
Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
  • Using the OutputCapture class, nothing seems to be shown in the console when using `Console.ReadKey()` - any suggestions? – derekantrican Jul 25 '19 at 16:12
-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'
Ben Hayward
  • 1,444
  • 23
  • 37
-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.

James
  • 1,471
  • 3
  • 23
  • 52