C# cmd中修改显示(显示进度变化效果)的方法
C# cmd中修改显示(显示进度变化效果)的方法
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:publicvoidPrintPercentage(intFinishedCount,intTotalCount){dec...

复制代码 代码如下:

public void PrintPercentage(int FinishedCount, int TotalCount)

{

decimal finishedPercentage = Convert.ToDecimal(FinishedCount) / Convert.ToDecimal(TotalCount);

Console.SetCursorPosition(0, Console.CursorTop - 1);

Console.WriteLine((finishedPercentage * 100).ToString("f1") + "%");

}

其中SetCursorPosition的目的就是重置光标到,里面参数的含义是(left, top),当前cmd最下面一行即为top.ToString("f1")是指保留一位小数.

或者用“r”也能达到目的,表示将光标回到当前第一行,如下:

复制代码 代码如下:

public void PrintPercentage(int FinishedCount, int TotalCount)

{

decimal finishedPercentage = Convert.ToDecimal(FinishedCount) / Convert.ToDecimal(TotalCount);

Console.WriteLine("r" + (finishedPercentage * 100).ToString("f1") + "%");

}

相比之下前一种更加灵活一点,可以定位到任何位置

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类