使用控制台在同一位置写入字符串。使用C#2.0编写

使用控制台在同一位置写入字符串。使用C#2.0编写,c#,.net,C#,.net,我在C#2.0中有一个控制台应用程序项目,需要在while循环中向屏幕写入一些内容。我不希望屏幕滚动,因为使用Console.Write或Console.Writeline方法将在控制台屏幕上不断递增地显示文本,因此它开始滚动 我想让字符串写在相同的位置。我该怎么做 谢谢使用设置位置。如果需要先确定,请使用and properties.函数写入循环的进度。循环计数器可用作x位置参数。这在第1行打印,根据您的需要进行修改 /// <summary> /// Writes

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

我想让字符串写在相同的位置。我该怎么做


谢谢

使用设置位置。如果需要先确定,请使用and properties.

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

    /// <summary>
    /// Writes a string at the x position, y position = 1;
    /// Tries to catch all exceptions, will not throw any exceptions.  
    /// </summary>
    /// <param name="s">String to print usually "*" or "@"</param>
    /// <param name="x">The x postion,  This is modulo divided by the window.width, 
    /// which allows large numbers, ie feel free to call with large loop counters</param>
    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) {
            }
        }
    }
//
///在x位置写入字符串,y位置=1;
///尝试捕获所有异常,但不会抛出任何异常。
/// 
///要打印的字符串通常为“*”或“@”
///x位置,这是模除以window.width,
///允许使用大的循环计数器进行呼叫
受保护的静态void WriteProgress(字符串s,int x){
int origRow=Console.CursorTop;
int origCol=Console.CursorLeft;
//Console.WindowWidth=10;//这是可行的。
int width=控制台窗口宽度;
x=x%宽度;
试一试{
控制台。设置光标位置(x,1);
控制台。写入;
}捕获(ArgumentOutOfRange异常){
}最后{
试一试{
控制台。设置光标位置(origRow、origCol);
}捕获(ArgumentOutOfRange异常){
}
}
}

非常有用,如果您已经“写”了一行,您可以继续执行
Console.SetCursorPosition(0,Console.CursorTop-1);控制台写入线(进度)至“从开始更新同一行…”。。。