软件编程
位置:首页>> 软件编程>> C#编程>> C# cmd中修改显示(显示进度变化效果)的方法

C# cmd中修改显示(显示进度变化效果)的方法

  发布时间:2023-11-02 13:55:34 

标签:cmd,修改,显示


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") + "%"); 


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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com