C# - Rewrite/edit 行,而程序是 运行

C# - Rewrite/edit line while program is running

有什么方法可以 edit/rewrite 某些已经由 Console.PrintLine() 方法打印的行?我必须能够编辑提示中显示的任何行。

这是我尝试获取代码的示例 运行,可能看起来像:

public static void RewriteLine(LineNr, Text)
{
    //Code
}

Console.WriteLine("Text to be rewritten");
Console.Writeline("Just some text");
RewriteLine(1, "New text");

根据先前代码的输出显示我要重写哪一行的示例:

要重写的文本 //这一行(已经被Console.WriteLine()方法执行过)应该替换为:"New text"

只是一些文字

它应该是这样的:

public static void RewriteLine(int lineNumber, String newText)
{
    int currentLineCursor = Console.CursorTop;
    Console.SetCursorPosition(0, currentLineCursor - lineNumber);
    Console.Write(newText); Console.WriteLine(new string(' ', Console.WindowWidth - newText.Length)); 
    Console.SetCursorPosition(0, currentLineCursor);
}

static void Main(string[] args)
{
    Console.WriteLine("Text to be rewritten");
    Console.WriteLine("Just some text");
    RewriteLine(2, "New text");
}

发生的事情是您更改了光标位置并在那里写了一些东西。您应该添加一些代码来处理长字符串。