7

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.

Community
  • 1
  • 1
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • http://stackoverflow.com/questions/888533/how-can-i-update-the-current-line-in-a-c-windows-console-app – H.B. Aug 13 '11 at 23:58

4 Answers4

7

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");
  }
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 2
    As EPLKleijntjens points out, this can leave part of a previous file name on the screen if the new file name is shorter. The sequence `ESCAPE [ K` , or `"\x1b[K"` will clear from the cursor to the end of the line *if* your terminal emulator supports it. – Keith Thompson Aug 14 '11 at 00:17
  • Alternative to Keith's comment is to write a line of spaces over the previous line prior to write the new content. If the console is 80 chars width, then --> Console.Write("{80 spaces}\r"); then write your text row with \r. This one is not subject to any terminal emulator compatibility. – Harps May 14 '13 at 05:53
4

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.

EPLKleijntjens
  • 916
  • 1
  • 8
  • 21
3

Use the Console.SetCursorPosition method: MSDN Console.SetCursorPosition

IAbstract
  • 19,551
  • 15
  • 98
  • 146
0

By default Console.WriteLine writes data to the console and terminates the line so you will not be able to over write the same line in this way

I would suggest that it is better to have a line written for each image worked on so that you can see which images have been worked on and what hasn't been

stack72
  • 8,198
  • 1
  • 31
  • 35
  • for (x = 0; x < images.Length - 1; x++) { for (y = x + 1; y < images.Length; y++) { Console.WriteLine("Working on file " + images[x] + " please wait"); – Daniel Lip Aug 13 '11 at 23:51
  • Even like this ok so it will not replace the current line but even now i see "Working on file 0001 please wait" and this i see on all the console window like many lines of the "working on file 001 please wait" then 0002 same and 0003. Instead of the first line working on file 0001 second line working on file 0002 and so on. Now what i see is Working on file 001 thne next line is Working on file 0001 and only after like 10-20 lines its changing to 002 – Daniel Lip Aug 13 '11 at 23:54