Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

showing percentage in .net console application

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?

like image 286
Ronnie Overby Avatar asked Sep 04 '09 13:09

Ronnie Overby


2 Answers

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);         }     } } 
like image 173
Jon Skeet Avatar answered Sep 22 '22 19:09

Jon Skeet


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();
like image 40
Noon Silk Avatar answered Sep 24 '22 19:09

Noon Silk