0

I visited pages like How to get all text from console to string?.

Is there a function to get all text in console and save it to string or array? I am thinking about getting text from console in function after writing the text. Is this possible? If not something that simulate the console functions like that I tried but including deleting text.

// I tried this. I do this for Write(),WriteLine(),ReadLine(),Read()
// It works for these functions. But when I remove something from console
// it don't save.
public static void Write(object value)
{
    Console.Write(value);
    AddToOutput(value.ToString());
}

public static void WriteLine(object value)
{
    Console.WriteLine(value);
    AddToOutput(value.ToString()+ "\n");
}

public static void WriteLine()
{
    Console.WriteLine();
    AddToOutput("\n");
}

public static string ReadLine()
{
    string txt = Console.ReadLine();
    AddToOutput(txt+"\n");
    return txt;
}

public static void AddToOutput(string txt)
{
    Log += txt;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dommilosz
  • 127
  • 13
  • How are you deleting the text? Changing the position and overwriting? – Ron Beyer Jan 06 '20 at 19:36
  • 2
    The question is not that clear; are you saying that you have a console program running in a console window, and you would like to *read* the text that was *already* in the console before you started your program? What exactly are you asking? If you want to know what text *you just wrote* to the console, you already know that; you wrote it! – Eric Lippert Jan 06 '20 at 19:37
  • Yes, I mean changing positions and overwriting. I want to get the text already written in console – dommilosz Jan 06 '20 at 19:52
  • Probably you should save the data to a variable or array as it gets written to the console. And as you move the cursor and change text in the console, also modify the data saved in the variable as well. – Andrew Jan 06 '20 at 20:25
  • There is no way to do that *directly* from C#, but you can do it indirectly by doing a PInvoke call to the Win32 API that reads the console's current contents. I've closed the question as a duplicate of an earlier similar question; see the answers to that question for a description of how to do the PInvoke steps to read from the console. – Eric Lippert Jan 06 '20 at 20:47

1 Answers1

1
Console.ReadKey();

You could perhaps use ReadKey to keep track of any backspace presses and then set the previous string as a variable to iterate over.

I'd probably use a combination of ReadLine() and ReadKey() to try to keep track of everything...perhaps combining them later on in the program.

Jeff Longo
  • 191
  • 1
  • 1
  • 13
  • Yes, but my problem is that the program changes the cursor position and replaces the text. Not user – dommilosz Jan 06 '20 at 20:20
  • In that case, I would be saving all console output to a text file, and then parsing it later for the data you need. https://stackoverflow.com/questions/4470700/how-to-save-console-writeline-output-to-text-file – Jeff Longo Jan 06 '20 at 20:25