How in c# console application on WriteLine i can do so it will replace each line of the same one in for loop? [duplicate]
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.
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");
}
}
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.
Use the Console.SetCursorPosition
method: MSDN Console.SetCursorPosition
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
精彩评论