C# clear Console last Item and replace new? Console Animation
Asked Answered
Y

7

24

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

Yokum answered 17/2, 2011 at 9:49 Comment(1)
The problem is same like[1], there are some ways to solve it. [1]: #889033Stannum
M
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);
         }
Microbiology answered 17/2, 2011 at 9:54 Comment(1)
I don't want to clear all Console. Just last one printed lineYokum
I
33

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);
}
Ignace answered 17/2, 2011 at 9:55 Comment(6)
If i set the start position (as start of current printed line)then I need to clear. But it will clear all screenYokum
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 functioningYokum
@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.Ignace
@digEmAii thanx, it is helpful but I need to customize it according to my codeYokum
I think using Console.Write(new string(' ', Console.WindowWidth - 1)) might be a bit faster than the loopDonegan
@Ray: yes probably, even if I doubt you can notice the difference :)Ignace
M
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);
         }
Microbiology answered 17/2, 2011 at 9:54 Comment(1)
I don't want to clear all Console. Just last one printed lineYokum
S
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"

Sochi answered 17/2, 2011 at 10:9 Comment(1)
This fails if the initial WriteLine wrapped around.Maximamaximal
C
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
Calvert answered 17/2, 2011 at 10:0 Comment(1)
hmm.. that looks charming. Let me try itYokum
D
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);
Dustin answered 21/2, 2013 at 13:1 Comment(0)
S
3

I think this is what you want ;)

Console.WriteLine("Searching file in...");
    foreach(var dir in DirList)
     {
       Console.Write(\"r" + dir);
     }
Sermonize answered 24/6, 2014 at 8:14 Comment(0)
C
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

Clearstory answered 24/9, 2011 at 2:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.