Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.Clear(); doesn't clean up the whole console

I have a part in my application that generates more output than in a console window fits. I'm satisfied with scrolling through the generated lines but by clearing the console window only the last part disappears. Everything above the height of the console window is still there when I'm scrolling up.

Now, is there a way to wipe the whole console content even the scrollback buffer? Or is the only solution to develop a pager?

Thanks for your replies!

EDIT:

I wrote the following code as POC:

for (int i = 0; i < 100; i++)
{
    Console.WriteLine(i);
}
Console.Clear();
Console.ReadKey();

When I'm running that code only the lines 71 to 100 will be deleted.

like image 842
Alex Avatar asked Oct 12 '25 11:10

Alex


1 Answers

Thanks for all your replies!

Finally I solved it with the following escape sequence:

Console.Clear();
Console.WriteLine("\x1b[3J");

This sequence removes the whole content of the console. (But it only works reliable if the clear command is called first)

After these two lines the console is empty and the scrollbars are removed or disabled. The cursor is on the second line (and there could be a few chars from the last input). To prevent that I called the clear command again.

I hope this will help someone with the same problem.

like image 89
Alex Avatar answered Oct 14 '25 01:10

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!