清除控制台应用程序中的特定行

joe*_*122 3 c# methods console console-application

我需要清除控制台中的某一行,但需要保留其余的行。我知道Console.Clear(),但会清除整个控制台。我如何只清除一行?

joe*_*122 5

我决定回答我自己的问题,因为我用谷歌搜索了这个,似乎没有人有方法。

由于没有 clearLine() / ClearLine() 方法,我做了一个。

这里是:

private static void clearLine()
{
        Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft));
}
Run Code Online (Sandbox Code Playgroud)

其他可能性:

private static void clearLine(int left, int top)
{    

int pLeft = Console.CursorLeft;
int pTop  = Console.CursorTop;

Console.setCursorPosition(left, top);
Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft));

Console.setCursorPosition(pLeft, pTop);
}
Run Code Online (Sandbox Code Playgroud)