C# 如何实现一个命令行进度条 - Console类的光标操作

来源:这里教程网 时间:2026-02-21 17:32:48 作者:

用 Console.SetCursorPosition 实现进度条核心逻辑

命令行进度条的关键不是重绘整行,而是复用同一行位置。C# 的 Console.SetCursorPosition(x, y) 允许你把光标“挪回去”,覆盖原有文字。比如进度条显示在第 10 行,每次更新前先调用

Console.SetCursorPosition(0, 10)
,再输出新内容,就能实现原地刷新,避免滚动刷屏。

构造一个简洁可复用的进度条方法

不需要第三方库,几行代码就能封装好。重点是:固定长度、支持百分比、自动覆盖、结尾不换行。示例如下:

(注意:需在循环中调用,且确保不被其他 Console.WriteLine 干扰)

定义总长度(如 50 字符),用
'█'
表示已完成部分,
'.'
或空格表示剩余
每次更新时计算当前填充宽度:
int filled = (int)Math.Round(progress * totalWidth)
拼出字符串:
new string('█', filled) + new string('.', totalWidth - filled)
调用
Console.SetCursorPosition(0, Console.CursorTop)
回到本行开头,再
Console.Write(...)
最后加
Console.Write($" {progress:P0}")
显示百分比,保持在同一行

避免光标错位和乱码的实用细节

Windows 控制台默认启用“快速编辑模式”,用户点一下就会暂停程序,导致光标位置异常。上线前建议关闭:

运行前加
Console.TreatControlCAsInput = true;
防止 Ctrl+C 中断光标逻辑
Console.OutputEncoding = System.Text.Encoding.UTF8;
确保 █ 等符号正常显示
若进度条后还有输出,记得在完成时补个
Console.WriteLine()
换行,否则后续日志会挤在进度条后面
多线程更新进度条?必须加锁或改用
Interlocked
更新进度值,再由主线程统一刷新

一个开箱即用的最小示例

复制粘贴就能跑,模拟 3 秒加载过程:

var total = 100;
for (int i = 0; i <= total; i++)
{
    double progress = (double)i / total;
    int width = 40;
    int filled = (int)Math.Round(progress * width);
    string bar = "[" + new string('█', filled) + new string(' ', width - filled) + "] ";
    Console.SetCursorPosition(0, Console.CursorTop);
    Console.Write($"{bar}{progress:P0}");
    System.Threading.Thread.Sleep(30); // 模拟工作
}
Console.WriteLine(); // 换行收尾

基本上就这些。不复杂但容易忽略光标定位和编码问题,调通一次,后续项目直接复用。

相关推荐