Harry Harry - 20 days ago 6
C# Question

How to modify the previous line of console text?

I'd like to achieve something like this:

Time consuming operation...OK
Another time consuming operation...
And another one, but it completed, so...OK


I displayed 3 line of text, each one related with a thread which can end sooner or later. But if the second one complete later than the third one, I'll get something like this:

Time consuming operation...OK
Another time consuming operation...
And another one, but it completed, so...OKOK


Which is of course unacceptable. I know how to go back in current line, but is there a way to go UP? I'd swear I've seen it somewhere, though it could be a Linux console :)

Forget it. See Far File Manager! It works in Windows console, it works even in PowerShell! How to make something like this? And the coolest part is it restores console state after exiting. So maybe I should ask - how to access console buffer directly? I assume I'll need some native code to do the trick, but maybe there's another way? I thought of clearing console with each update, but this seems like overkill. Or maybe it isn't? Will it blink?

Answer

You can move cursor wherever you want: Console.SetCursorPosition or use Console.CursorTop.

Console.SetCursorPosition(0, Console.CursorTop -1);
Console.WriteLine("Over previous line!!!");
Comments