C#-在程序运行时重写/编辑行

本文关键字:编辑 重写 运行时 程序 C#- | 更新日期: 2023-09-27 18:27:43

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

这是一个我试图运行的代码的例子,可能看起来像:

public static void RewriteLine(LineNr, Text)
{
    //Code
}
Console.WriteLine("Text to be rewritten");
Console.Writeline("Just some text");
RewriteLine(1, "New text");

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

要重写的文本//此行(已由Console.WriteLine()方法执行bin)应替换为:"New Text"

只有一些文本

C#-在程序运行时重写/编辑行

它应该是这样的:

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");
}

现在的情况是,你改变光标的位置,在那里写一些东西。您应该添加一些处理长字符串的代码。