showing percentage in .net console application

Ronnie Overby picture Ronnie Overby · Sep 4, 2009 · Viewed 20.9k times · Source

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?

Answer

Jon Skeet picture Jon Skeet · Sep 4, 2009

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);
        }
    }
}