C#清除控制台最后一项并更换新的?控制台动画

C#清除控制台最后一项并更换新的?控制台动画,c#,console.writeline,C#,Console.writeline,以下CSharp代码(仅示例): 将输出打印为: 在中搜索文件。。。 dir1 dir2 dir3 dir4 . . . 问题? 我怎样才能得到输出 在中搜索文件。。。 dir1 (然后清除dir1并打印dir2等)所有下一个dir名称将替换上一个dir 如果您的问题是清除控制台,则使用方法console.Clear(),如果不是,则使用此代码覆盖最后一行 Console.WriteLine("Searching file in..."); foreach(var dir i

以下CSharp代码(仅示例):

将输出打印为:

在中搜索文件。。。
dir1
dir2
dir3
dir4
.
.
.
问题? 我怎样才能得到输出

在中搜索文件。。。
dir1
(然后清除dir1并打印dir2等)所有下一个dir名称将替换上一个dir


如果您的问题是清除控制台,则使用方法
console.Clear(),如果不是,则使用此代码覆盖最后一行

Console.WriteLine("Searching file in...");
        foreach(var dir in DirList)
         {
           Console.SetCursorPosition(1,0);
           Console.WriteLine(dir);
         }
用于将光标设置在最后一行的开头并重写它

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

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

编辑:

根据您的评论,您可以做如下操作:

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

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);
}
公共静态无效ClearCurrentConsoleLine()
{
int currentLineCursor=Console.CursorTop;
Console.SetCursorPosition(0,Console.CursorTop);
对于(int i=0;i
只需保存
控制台.CursorLeft
控制台.CursorTop
属性的值,即可跟踪光标的当前位置。然后写入、重置和重复。或者更确切地说,在这种情况下,重置、写入并重复

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
您可以使用“\r”打印,这样光标就不会跳行,您可以重写它

foreach(var dir in DirList)
     {
       Console.Write("\r{0}%           ",dir);
     }
在数字后使用空格以确保所有内容都已擦除,并使用.Write而不是WriteLine 因为您不想添加“\n”

这将满足您的要求:

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

基本上与@digEmAll建议的相同,但它使用了实际的字符而不是控制台。WindowWidth

虽然这是一篇非常古老的文章,但我将发布我的方法。也许这会对某人有所帮助

Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new String(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(dir);

我想这就是你想要的;)


我不想清除所有控制台。仅最后一行如果我设置开始位置(作为当前打印行的开始),则需要清除。但它会清除所有屏幕,因为您的代码对于该代码来说运行良好。但在我的实际代码中,我触发了一个事件foreach dir name,它将当前dir name传递给事件处理程序,该事件处理程序将打印当前项。我正在eventHandler中使用consolewriteline。我尝试了相同的代码,但它不是functioning@GAPS:您可能只需要在eventhandler中调用
ClearCurrentConsoleLine
Write
(而不是
WriteLine
),但我不能确定,因为我不清楚您的代码。无论如何,我认为您可以轻松地调整代码以满足您的需要,您只需要了解它是如何工作的,我保证,这非常简单。@digEmAii-thanx,这很有用,但我需要根据我的代码自定义它。我认为使用
Console.Write(新字符串(“”,Console.WindowWidth-1))
可能比loophmm快一点。。那看起来很迷人。让我试试这个问题和[1]一样,有一些方法可以解决它。[1] :如果初始写线被环绕,则此操作失败。
Console.WriteLine("Searching file in...");
    foreach(var dir in DirList)
     {
       Console.Write(\"r" + dir);
     }