Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

c# - How to modify the previous line of console text?

I'd like to achieve something like this:

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

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...