【问题标题】:Get text that already exists in console in C# [duplicate]在 C# 中获取控制台中已存在的文本 [重复]
【发布时间】:2021-02-23 05:32:47
【问题描述】:

我正在尝试创建一个机制,当我的程序完成可能需要一段时间的任务时,它会打印任务完成的百分比(例如完成 30% 到 31%)。我已经把百分比计算下来了,但没有打印出来。我想我必须获取屏幕上已经存在的文本才能做到这一点。

【问题讨论】:

  • Console.GetCursorPosition / Console.SetCursorPosition 然后在上面写

标签: c#


【解决方案1】:

加载显示的简单示例。它有一些闪烁,但我认为花更多的时间可以解决这个问题

class Program
    {
        static void Main()
        {
            for (int i = 0; i < 20; i++)
            {
                WriteMessage($"Some text, {i}");
                ShowPercentage(i);
                Thread.Sleep(100);
            }

            Console.CursorTop = Console.WindowHeight - 2;
            Console.Write("                        ");
            Console.CursorLeft = 0;
            Console.WriteLine("Done");
        }

        static void WriteMessage(string msg)
        {
            Console.CursorTop = Console.WindowHeight - 2;
            Console.WriteLine(msg);
        }

        static void ShowPercentage(int value)
        {
            Console.CursorTop = Console.WindowHeight - 1;
            Console.WriteLine($"Loaded {value}%");
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 2011-11-25
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多