使用C#2.0中的Console.Write在同一位置写入字符串

我在C#2.0中有一个控制台应用程序项目,需要在while循环中向屏幕写入内容。 我不希望屏幕滚动,因为使用Console.Write或Console.Writeline方法将继续以增量方式在控制台屏幕上显示文本,从而开始滚动。

我想把字符串写在同一个位置。 我怎样才能做到这一点?

谢谢

使用Console.SetCursorPosition设置位置。 如果需要先确定它,请使用Console.CursorLeft和Console.CursorTop属性。

写入循环进度的函数。 您的循环计数器可用作x位置参数。 这将在第1行打印,根据您的需要进行修改。

  ///  /// Writes a string at the x position, y position = 1; /// Tries to catch all exceptions, will not throw any exceptions. ///  /// String to print usually "*" or "@" /// The x postion, This is modulo divided by the window.width, /// which allows large numbers, ie feel free to call with large loop counters protected static void WriteProgress(string s, int x) { int origRow = Console.CursorTop; int origCol = Console.CursorLeft; // Console.WindowWidth = 10; // this works. int width = Console.WindowWidth; x = x % width; try { Console.SetCursorPosition(x, 1); Console.Write(s); } catch (ArgumentOutOfRangeException e) { } finally { try { Console.SetCursorPosition(origRow, origCol); } catch (ArgumentOutOfRangeException e) { } } }