如何修改上一行控制台文本?

How to modify the previous line of console text?(如何修改上一行控制台文本?)
本文介绍了如何修改上一行控制台文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现这样的目标:

Time consuming operation...OK
Another time consuming operation...
And another one, but it completed, so...OK

我显示了 3 行文本,每行都与一个线程相关,该线程迟早会结束.但如果第二个比第三个晚完成,我会得到这样的结果:

I displayed 3 line of text, each one related with a thread which can end sooner or later. But if the second one complete later than the third one, I'll get something like this:

Time consuming operation...OK
Another time consuming operation...
And another one, but it completed, so...OKOK

这当然是不可接受的.我知道如何返回当前行,但有没有办法向上?我发誓我在某个地方见过它,虽然它可能是一个 Linux 控制台 :)

Which is of course unacceptable. I know how to go back in current line, but is there a way to go UP? I'd swear I've seen it somewhere, though it could be a Linux console :)

算了.请参阅远文件管理器!它适用于 Windows 控制台,甚至适用于 PowerShell!如何制作这样的东西?最酷的部分是它在退出后恢复控制台状态.所以也许我应该问 - 如何直接访问控制台缓冲区?我想我需要一些本机代码才能做到这一点,但也许还有另一种方法?我想在每次更新时清除控制台,但这似乎有点矫枉过正.或者也许不是?它会闪烁吗?

Forget it. See Far File Manager! It works in Windows console, it works even in PowerShell! How to make something like this? And the coolest part is it restores console state after exiting. So maybe I should ask - how to access console buffer directly? I assume I'll need some native code to do the trick, but maybe there's another way? I thought of clearing console with each update, but this seems like overkill. Or maybe it isn't? Will it blink?

推荐答案

您可以随意移动光标:Console.SetCursorPosition 或使用 Console.CursorTop.

You can move cursor wherever you want: Console.SetCursorPosition or use Console.CursorTop.

Console.SetCursorPosition(0, Console.CursorTop -1);
Console.WriteLine("Over previous line!!!");

这篇关于如何修改上一行控制台文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)