Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# clear Console last Item and replace new? Console Animation

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

like image 730
PawanS Avatar asked Feb 17 '11 09:02

PawanS


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C language?

C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

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);
}
like image 77
digEmAll Avatar answered Sep 29 '22 11:09

digEmAll


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);
         }
like image 35
The GiG Avatar answered Sep 29 '22 11:09

The GiG


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"

like image 41
David Pires Avatar answered Sep 29 '22 10:09

David Pires