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

发布于 2024-10-18 00:11:19 字数 497 浏览 1 评论 0原文

以下 CSharp 代码(仅示例):

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

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

将输出打印为:

Searching file in...

dir1

dir2

dir3

dir4

.

.

.

问题? 我怎样才能得到输出

Searching file in...

dir1  

(然后清除dir1并打印dir2等)所有下一个目录名称将替换之前的目录

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

妄想挽回 2024-10-25 00:11:19

使用 Console.SetCursorPosition将光标设置在最后一行的开头并重写它。

类似于:

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

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);
}
薄凉少年不暖心 2024-10-25 00:11:19

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

Console.WriteLine("Searching file in...");
        foreach(var dir in DirList)
         {
           Console.SetCursorPosition(1,0);
           Console.WriteLine(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);
         }
半仙 2024-10-25 00:11:19

你可以使用“\r”进行打印,这样光标就不会跳行,并且可以重写它。

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

在数字后使用空格以确保所有内容都被删除,并使用 .Write 而不是 WriteLine
因为你不想添加“\n”

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"

鯉魚旗 2024-10-25 00:11:19

只需保存 Console.CursorLeftConsole.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

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
谁许谁一生繁华 2024-10-25 00:11:19

虽然这是一篇很旧的帖子,但我会发布我的方法。也许这会对某人有帮助

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

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);
心碎无痕… 2024-10-25 00:11:19

我想这就是你想要的;)

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

I think this is what you want ;)

Console.WriteLine("Searching file in...");
    foreach(var dir in DirList)
     {
       Console.Write(\"r" + dir);
     }
私藏温柔 2024-10-25 00:11:19

这将实现我认为你所要求的:

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

基本上与@digEmAll建议的相同,但它使用实际的字符数而不是Console.WindowWidth

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文