How can I update the current line in a C# Windows Console App?

前端 未结 17 1256
南旧
南旧 2020-11-22 14:43

When building a Windows Console App in C#, is it possible to write to the console without having to extend a current line or go to a new line? For example, if I want to sho

17条回答
  •  感情败类
    2020-11-22 15:19

    Explicitly using a Carrage Return (\r) at the beginning of the line rather than (implicitly or explicitly) using a New Line (\n) at the end should get what you want. For example:

    void demoPercentDone() {
        for(int i = 0; i < 100; i++) {
            System.Console.Write( "\rProcessing {0}%...", i );
            System.Threading.Thread.Sleep( 1000 );
        }
        System.Console.WriteLine();    
    }
    

提交回复
热议问题