Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read values already printed to the console

Tags:

c#

console

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.

like image 671
Danny Goodall Avatar asked Jan 21 '16 09:01

Danny Goodall


People also ask

What value does console read () returns?

Return Value: It returns the next character from the input stream, or a negative one (-1) if there are currently no more characters to be read.

What is console read ()?

Console.Read() is a method that is used to read the next character from the standard input stream. Console.readline() is a method that is used to read the next line of characters from the standard input stream. Its syntax is -: public static int Read ();

What is the line to print output to the console?

To print a message to the console, we use the WriteLine method of the Console class. The class represents the standard input, output, and error streams for console applications. Note that Console class is part of the System namespace. This line was the reason to import the namespace with the using System; statement.

How do I see console write in Visual Studio?

In Visual Studio uppermost menu choose Debug > Windows > Output. It shows all Console. WriteLine("Debug MyVariable: " + MyVariable) when you get to them.


2 Answers

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.

like image 138
Irshad Avatar answered Sep 30 '22 14:09

Irshad


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/

like image 45
Tom Chantler Avatar answered Sep 30 '22 13:09

Tom Chantler