简体   繁体   中英

Read values already printed to the 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.

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 .

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/

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'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM