进度条核心是用Console.SetCursorPosition(0, y)复位光标原地刷新,固定长度、百分比显示、自动覆盖;需禁用快速编辑、设置UTF8编码、完成时换行,多线程需同步。
命令行进度条的关键不是重绘整行,而是复用同一行位置。C# 的 Console.SetCursorPosition(x, y) 允许你把光标“挪回去”,覆盖原有文字。比如进度条显示在第 10 行,每次更新前先调用 Console.SetCursorPosition(0, 10),再输出新内容,就能实现原地刷新,避免滚动刷屏。
不需要第三方库,几行代码就能封装好。重点是:固定长度、支持百分比、自动覆盖、结尾不换行。示例如下:
(注意:需在循环中调用,且确保不被其他 Console.WriteLine 干扰)
'█' 表示已完成部分,'.' 或空格表示剩余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(); // 换行收尾
基本上就这些。不复杂但容易忽略光标定位和编码问题,调通一次,后续项目直接复用。