When building a Windows Console App in C#, is it possible to write to the console without having to extend a current line or go to a new line? For example, if I want to show a percentage representing how close a process is to completion, I'd just like to update the value on the same line as the cursor, and not have to put each percentage on a new line.
Can this be done with a "standard" C# console app?
Here's another one :D
If you print only
"\r"
to the console the cursor goes back to the beginning of the current line and then you can rewrite it. This should do the trick:Notice the few spaces after the number to make sure that whatever was there before is erased.
Also notice the use of
Write()
instead ofWriteLine()
since you don't want to add an "\n" at the end of the line.\r
is used for this scenarios.\r
represents a carriage return which means the cursor returns to the start of the line.That's why windows uses
\n\r
as it's new line marker.\n
moves you down a line, and\r
returns you to the start of the line.