控制台状态栏

时间:2014-08-26 05:30:03

标签: c#

我有一个函数可以使用以下语句更新控制台应用程序中的状态栏:

Console.writeline("Status {0} Copied", i);

上述陈述的问题是,过了一段时间,它会用语句填充控制台行:

Status 1 Copied
Status 2 Copied

等等。

有没有办法让它只刷新一行,这样1就会被2替换,所以它不会以文本墙结束?

5 个答案:

答案 0 :(得分:3)

是的,这是完全可能的,您只需要跟踪您所在的行,然后覆盖该行:

   Console.Write("0000000000");
   Console.SetCursorPosition(3, 0);
   Console.Write("55");
   Console.ReadLine();

输出:005500000

Console.WriteLine("0000000000");
Console.WriteLine("1111111111");
Console.WriteLine("2222222222");

Console.SetCursorPosition(3, 1);
Console.Write("55");
Console.ReadLine();

输出:

0000000000

1155111111

2222222222

答案 1 :(得分:3)

要在当前行的开头重复写入输出,请尝试:

private static void SetStatusLineValue(int value)
{
    Console.SetCursorPosition(0, Console.CursorTop);
    Console.Write("\rStatus {0} Copied", value);
}

甚至简单地说:Console.Write("\rStatus {0} Copied", value);

要获得一个更灵活的解决方案,要么会记得'起始行,或使用指定的行:

private static void SetTextForLine(string text, ref int line)
{
    //set the status line for future reference
    if (line < 0)
    {
        line = Console.CursorTop;
    }

    //save line/cursor state
    int currentLine = Console.CursorTop;
    bool cursorVisible = Console.CursorVisible;
    Console.SetCursorPosition(0, line);
    Console.WriteLine(text);

    //restore state
    Console.CursorTop = (currentLine == line ? currentLine + 1 : currentLine);
    Console.CursorVisible = cursorVisible;
}

用法:

int statusLine = -1;
for (int i = 0; i < 10; i++)
{
    Console.WriteLine("Other lines in here...");

    DoLongRunningOperation();

    //update the status line here
    SetTextForLine("Status " + i + " Copied", ref statusLine);
}

输出:

Other lines in here...
Status 9 Copied            <- this line updating each SetTextForLine() call
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...

答案 2 :(得分:1)

您还可以使用 Console.MoveBufferArea 方法滚动控制台输出。

static void Main(string[] args)
{
  Console.SetBufferSize(Console.WindowWidth, Console.WindowHeight);

  Console.CursorLeft = 0;
  Console.CursorTop = 0;

  Console.BackgroundColor = ConsoleColor.White;
  Console.ForegroundColor = ConsoleColor.Black;
  Console.Write("This is the statusbar".PadRight(Console.WindowWidth, ' '));

  Console.BackgroundColor = ConsoleColor.Black;
  Console.ForegroundColor = ConsoleColor.White;

  int line = 1;
  int totalLines = 1;
  while (true) {
    Thread.Sleep(100);

    if (line == Console.WindowHeight){
      // Scoll one line:
      Console.MoveBufferArea(0, 2, Console.WindowWidth, Console.WindowHeight - 2, 0, 1);
      line--;
    }
    Console.CursorLeft = 0;
    Console.CursorTop = line;
    Console.Write(string.Format("This is line {0}", totalLines));

    totalLines++;
    line++;
  }
}

答案 3 :(得分:0)

没有可能的方法,但您可以尝试一个解决方案来清除控制台屏幕,然后才会写下一条消息。

Console.Clear();

您可以尝试另一个:"\r"返回当前行的开头并重新编写文本。

Console.Write("\rStatus {0} copied", i);

否则你可以使用WinForms

答案 4 :(得分:0)

我接受了答案,因为它帮助我制作了以下代码:

Console.WriteLine("Before Output");
for (int i = 0; i < 10; i++)
     {
          int currentLine = Console.CursorTop;
          Console.WriteLine("i is {0}", i);
          Thread.Sleep(500);
          Console.SetCursorPosition(0, currentLine);
     }
Console.WriteLine("After");

这是我将要使用的代码。