Get all console buffer

0

How can I get all of the Console text ( System.Console ) to a String , without redirecting console output to a process , for example, I want to get what is in Console through the Console itself. It is possible? If so, how do I do this?

    
asked by anonymous 08.05.2016 / 04:53

1 answer

1

See this example from here:

using System;
using System.IO;

namespace nsStreams
{
  public class Redirect
  {
    static public void Main ()
    {
        FileStream ostrm;
        StreamWriter writer;
        TextWriter oldOut = Console.Out;
        try
        {
            ostrm = new FileStream ("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
            writer = new StreamWriter (ostrm);
        }
        catch (Exception e)
        {
            Console.WriteLine ("Cannot open Redirect.txt for writing");
            Console.WriteLine (e.Message);
            return;
        }
        Console.SetOut (writer);
        Console.WriteLine ("This is a line of text");
        Console.WriteLine ("Everything written to Console.Write() or");
        Console.WriteLine ("Console.WriteLine() will be written to a file");
        Console.SetOut (oldOut);
        writer.Close();
        ostrm.Close();
        Console.WriteLine ("Done");
      }
   }
}

SetOut details here

Source: link

    
14.05.2016 / 21:47