Boris Eetgerink

June 22, 2023

From 0 to 100% on a single line

Or how to display a progress indicator in a console app:

// Avoid a blinking cursor.
Console.CursorVisible = false;

Console.WriteLine("Downloading...");
for (int i = 0; i <= 100; i++) {
    // Overwrite the output on the current line.
    Console.CursorLeft = 0;
    Console.Write($"Progress: {i.ToString(),3}%");
    await Task.Delay(50);
}

// Terminate the progress line.
Console.WriteLine();
Console.WriteLine("Download complete.");

Output:

Downloading...
Progress: 100%
Download complete.

That's all!

About Boris Eetgerink

Hey, this is my C# .NET blog. I don't have a comments system, but you can contact me at boriseetgerink at hey dot com.