33

I have a console app that performs a lengthy process.

I am printing out the percent complete on a new line, for every 1% complete.

How can I make the program print out the percent complete in the same location in the console window?

Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
  • See also this related question: http://stackoverflow.com/questions/888533/how-can-i-update-the-current-line-in-a-c-windows-console-app – Dirk Vollmar Sep 04 '09 at 14:27
  • I think the backspace method is perhaps the nicest (and most portable), though would require slightly more work. – Noon Silk Sep 04 '09 at 14:35

2 Answers2

81

Print \r to get back to the start of the line (but don't print a newline!)

For example:

using System;
using System.Threading;

class Test
{
    static void Main()
    {
        for (int i=0; i <= 100; i++)
        {
            Console.Write("\r{0}%", i);
            Thread.Sleep(50);
        }
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 7
    Yep, this is the easiest way to do it. Make sure (if you are using it for strings of variable length) that you remember to overwrite with spaces at the end of lines. e.g. replacing "99% complete" with "Done!" will show the line "Done!omplete" - not ideal. :) – ZombieSheep Sep 04 '09 at 14:03
  • Jon Skeet: Sorry, it seems stack overflow won't let me rollback ronnies edit to your post correctly. Please attempt it yourself. – Noon Silk Sep 04 '09 at 14:09
  • 1
    It may be boringly worth noting that this code won't work when run on an operating system that isn't Windows. I'd recommend the `SecCursorPosition` approach. – Noon Silk Sep 04 '09 at 14:11
  • 1
    @silky: I've just run it on Linux with Mono, and it worked with no problems. However, using your suggestion didn't - because it put the percentage at the top of the screen, rather than on the same line as "Hello : ". That's fixable of course, but it does show the advantage of a simple solution :) – Jon Skeet Sep 04 '09 at 14:23
  • Okay, I'll still hazard a guess that on a Mac it'll go to a new line. Granted, though, the SecCursorPos doesn't work unless you also set the right row. – Noon Silk Sep 04 '09 at 14:28
  • @silky: On MacOS X I'd expect it to be fine - it's only MacOS 9 that treats \r as a newline, I believe. Are there any implementations of .NET for MacOS 9? – Jon Skeet Sep 04 '09 at 14:45
  • 1
    If you're printing a variable length string (i.e. decimal places or % can both inrease or decrease), you can add alignment into the format string: `"{0,3}%"` (aligned right up to 3 spaces), add placeholder 0's `"{0:000.0}%"` or add trailing spaces to cover up the exposed characters `"{0}% _ _ _ _ _"` – Dead.Rabit Nov 07 '12 at 16:53
20

You may use:

Console.SetCursorPosition();

To set the position appropriately.

Like so:

Console.Write("Hello : ");
for(int k = 0; k <= 100; k++){
    Console.SetCursorPosition(8, 0);
    Console.Write("{0}%", k);
    System.Threading.Thread.Sleep(50);
}

Console.Read();
Noon Silk
  • 54,084
  • 6
  • 88
  • 105