English 中文(简体)
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

最佳回答

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);
         }
问题回答

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);
}

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

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

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

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

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);

I think this is what you want ;)

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

This will do what I think you re asking for:

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

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签