How can I update the current line in a C Windows Console App

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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?

Answers

If you print only " " 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:

for(int i = 0; i < 100; ++i)
{
    Console.Write("
{0}%   ", i);
}

Notice the few spaces after the number to make sure that whatever was there before is erased.
Also notice the use of Write() instead of WriteLine() since you don t want to add an " " at the end of the line.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/888533/how-can-i-update-the-current-line-in-a-c-sharp-windows-console-app

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils