22

The following CSharp Code(just sample):

Console.WriteLine("Searching file in...");

foreach(var dir in DirList)
{
    Console.WriteLine(dir);
}

Prints Output As:

Searching file in...

dir1

dir2

dir3

dir4

.

.

.

Question? How can I get the output as

Searching file in...

dir1  

(then clear dir1 and print dir2 and so on)All next dir name wiil replace the previous dir

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
PawanS
  • 7,033
  • 14
  • 43
  • 71
  • The problem is same like[1], there are some ways to solve it. [1]: http://stackoverflow.com/questions/888533/how-can-i-update-the-current-line-in-a-c-sharp-windows-console-app/11322943#11322943 – lisunde Jul 04 '12 at 05:44

7 Answers7

31

Use Console.SetCursorPosition to set the cursor on the start of the last line and rewrite it.

Something like:

Console.WriteLine(dir);
Console.SetCursorPosition(0, Console.CursorTop - 1);

EDIT:

According to your comment, you could do as follows:

Console.WriteLine("Searching file in...");
foreach (var dir in DirList)
{
    ClearCurrentConsoleLine();
    Console.Write(dir);
}

With ClearCurrentConsoleLine defined as:

public static void ClearCurrentConsoleLine()
{
    int currentLineCursor = Console.CursorTop;
    Console.SetCursorPosition(0, Console.CursorTop);
    for (int i = 0; i < Console.WindowWidth; i++)
        Console.Write(" ");
    Console.SetCursorPosition(0, currentLineCursor);
}
digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • If i set the start position (as start of current printed line)then I need to clear. But it will clear all screen – PawanS Feb 17 '11 at 10:01
  • Your code is working fine for this code. But in my actuall code I am triggering a event foreach dir name and that passes the current dir name to event handler and that event handler prints the current Item. I am using consolewriteline in eventHandler. I tried same code but its not functioning – PawanS Feb 17 '11 at 10:21
  • @GAPS: Probably you just need to call `ClearCurrentConsoleLine` and `Write` (not `WriteLine`) in the eventhandler, but I can't be sure because I don't know exaclty your code. Anyway I think you can easily adjust the code to fit your needs, you just need to figure out how it works and, I assure, that's really simple. – digEmAll Feb 17 '11 at 10:27
  • @digEmAii thanx, it is helpful but I need to customize it according to my code – PawanS Feb 17 '11 at 12:58
  • I think using `Console.Write(new string(' ', Console.WindowWidth - 1))` might be a bit faster than the loop – Ray Feb 08 '12 at 14:26
  • @Ray: yes probably, even if I doubt you can notice the difference :) – digEmAll Feb 09 '12 at 21:56
17

If your problem is clearing the console then use the method Console.Clear();, if it's not, use this to overwrite the last line;

Console.WriteLine("Searching file in...");
        foreach(var dir in DirList)
         {
           Console.SetCursorPosition(1,0);
           Console.WriteLine(dir);
         }
zvava
  • 101
  • 1
  • 3
  • 14
The GiG
  • 2,571
  • 2
  • 28
  • 29
12

You could print using "\r", that way cursor dont jump a line, and you can rewrite it.

foreach(var dir in DirList)
     {
       Console.Write("\r{0}%           ",dir);
     }

use spaces after number to make sure everything is erased, and use .Write instead of WriteLine because you dont want to add "\n"

David Pires
  • 346
  • 3
  • 13
6

Just keep track of the current position of the cursor by saving the values of the Console.CursorLeft and Console.CursorTop properties. Then write, reset and repeat. Or rather in this case, reset, write and repeat.

Console.WriteLine("Searching file in...");

// save the current cursor position
var cursorLeft = Console.CursorLeft;
var cursorTop = Console.CursorTop;

// build a format string to establish the maximum width do display
var maxWidth = 60;
var fmt = String.Format("{{0,-{0}}}", maxWidth);

foreach (var dir in dirList)
{
    // restore the cursor position
    Console.SetCursorPosition(cursorLeft, cursorTop);

    // trim the name if necessary
    var name = Path.GetFileName(dir);
    if (name.Length > maxWidth)
        name = name.Substring(0, maxWidth);

    // write the trimmed name
    Console.Write(fmt, name);

    // do some work
}
Console.WriteLine(); // end the previous line
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
3

Although this is a quite old post, I will post my approach. Maybe this would help someone

Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new String(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(dir);
Alpay
  • 1,350
  • 2
  • 24
  • 56
3

I think this is what you want ;)

Console.WriteLine("Searching file in...");
    foreach(var dir in DirList)
     {
       Console.Write(\"r" + dir);
     }
user3770256
  • 121
  • 3
1

This will do what I think you're asking for:

string s = "\r";
s += new string(' ', Console.CursorLeft);
s += "\r";
Console.Write(s);

Essentially the same as @digEmAll suggested, but it uses the actual # of chars instead of Console.WindowWidth

dlchambers
  • 3,511
  • 3
  • 29
  • 34