如何更新在C#的Windows控制台应用程序的当前行? [英] How can I update the current line in a C# Windows Console App?

查看:261
本文介绍了如何更新在C#的Windows控制台应用程序的当前行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中建立一个Windows控制台应用程序,是否有可能写入控制台,而无需延长当前行或者去到一个新行?例如,如果我要显示一个百分比再presenting的过程是多么接近完成,我只是想以更新的同一行光标的价值,而不是必须把每个百分比上一个新的行。

When building a Windows Console App in C#, is it possible to write to the console without having to extend a current line or go to a new line? For example, if I want to show a percentage representing how close a process is to completion, I'd just like to update the value on the same line as the cursor, and not have to put each percentage on a new line.

可以这样用标准C#控制台应用程序做了什么?

Can this be done with a "standard" C# console app?

推荐答案

如果您只打印为\\ r控制台光标追溯到的开始时当前行,那么你可以重写。这应该做的伎俩:

If you print only "\r" to the console the cursor goes back to the begining of the current line and then you can rewrite it. This should do the trick:

for(int i = 0; i < 100; ++i)
{
    Console.Write("\r{0}%   ", i);
}

注意号码后的几个空格,以确保无论是有之前被删除。

还要注意)使用的写(而不是的WriteLine(),因为你不希望添加一个\\ N在该行的末尾。

Notice the few spaces after the number to make sure that whatever was there before is erased.
Also notice the use of Write() instead of WriteLine() since you don't want to add an "\n" at the end of the line.

这篇关于如何更新在C#的Windows控制台应用程序的当前行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆