Clearing a line in the console

后端 未结 4 1531
青春惊慌失措
青春惊慌失措 2021-02-10 10:50

How can a line in the console be cleared in C#?

I know how to place the cursor at the beginning of a line:

Console.SetCursorPosition(0, Console.CursorTop         


        
4条回答
  •  一生所求
    2021-02-10 11:08

    Once the last space of a console buffer row is used, the console cursor automatically jumps to the next line.

    1. Reset cursor back to the beginning before it reaches edge of console
    2. Erase old console output, placing cursor on next line
    3. Reset cursor back onto the line that was just cleared

      while (true)
      {
        Console.Write(".");
        if (Console.CursorLeft + 1 >= Console.BufferWidth)
        {
          Console.SetCursorPosition(0, Console.CursorTop);
          Console.Write(Enumerable.Repeat(' ', Console.BufferWidth).ToArray());
          Console.SetCursorPosition(0, Console.CursorTop - 1);
        }
      
        if (Console.KeyAvailable)
          break;
      }
      

提交回复
热议问题