Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How in c# console application on WriteLine i can do so it will replace each line of the same one in for loop? [duplicate]

Tags:

c#

Possible Duplicate:
How can I update the current line in a C# Windows Console App?

What i mean is that i have a for in a for loops.

For (x=0; x<this.Length;x++)
     { 
     for (y=0; y<this.Length;y++)
          {
           Console.WriteLine("Working on file " + images[x] + " please wait");
          }
     }

The line Console.WriteLine("Working on file " + images[x] + " please wait"); Will write in the console window each images[x] file line under line. I want that it will write it once and then overwrite the same line and so on. Not line under line . Like counting "Working on file 0001 please wait" Then next line will replace the same one "working on file 0002 please wait"

I tried to put Console.Clear(); after the Console.WriteLine but then its like blinking doestn work smooth.

like image 417
Daniel Lip Avatar asked Aug 13 '11 23:08

Daniel Lip


2 Answers

Instead of using WriteLine which writes a carrage return and a line feed, only write the carrage return. That places the cursor at the beginning of the line, so that next line it written on top of it:

for (x=0; x<this.Length;x++) { 
  for (y=0; y<this.Length;y++) {
    Console.Write("Working on file " + images[x] + " please wait\r");
  }
}
like image 78
Guffa Avatar answered Sep 27 '22 16:09

Guffa


You can use Console.Write and prepend \r like this:

Console.Write("\rWorking on file " + images[x] + " please wait");

And perhaps append a few spaces at the end too, just to make sure you erase previous content.

like image 42
EPLKleijntjens Avatar answered Sep 27 '22 17:09

EPLKleijntjens