C# 清除控制台最后一个项目并替换新的?控制台动画
以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
Console.SetCursorPosition
将光标设置在最后一行的开头并重写它。类似于:
编辑:
根据您的评论,您可以执行以下操作:
将
ClearCurrentConsoleLine
定义为:Use
Console.SetCursorPosition
to set the cursor on the start of the last line and rewrite it.Something like:
EDIT:
According to your comment, you could do as follows:
With
ClearCurrentConsoleLine
defined as:如果您的问题是清除控制台,则使用方法
Console.Clear();
,如果不是,请使用此方法覆盖最后一行;If your problem is clearing the console then use the method
Console.Clear();
, if it's not, use this to overwrite the last line;你可以使用“\r”进行打印,这样光标就不会跳行,并且可以重写它。
在数字后使用空格以确保所有内容都被删除,并使用 .Write 而不是 WriteLine
因为你不想添加“\n”
You could print using "\r", that way cursor dont jump a line, and you can rewrite it.
use spaces after number to make sure everything is erased, and use .Write instead of WriteLine
because you dont want to add "\n"
只需保存
Console.CursorLeft
和Console.CursorTop
属性的值即可跟踪光标的当前位置。然后写入、重置并重复。或者更确切地说,在这种情况下,重置、写入并重复。Just keep track of the current position of the cursor by saving the values of the
Console.CursorLeft
andConsole.CursorTop
properties. Then write, reset and repeat. Or rather in this case, reset, write and repeat.虽然这是一篇很旧的帖子,但我会发布我的方法。也许这会对某人有帮助
Although this is a quite old post, I will post my approach. Maybe this would help someone
我想这就是你想要的;)
I think this is what you want ;)
这将实现我认为你所要求的:
基本上与@digEmAll建议的相同,但它使用实际的字符数而不是Console.WindowWidth
This will do what I think you're asking for:
Essentially the same as @digEmAll suggested, but it uses the actual # of chars instead of Console.WindowWidth