1

How can I update the current line in a C# Windows Console App? provides several options on how a line of text written to the console can be refreshed. Unfortunately, none of the main options work correctly if the console app's output is redirected to a file.

 MyApp.exe > outfile.txt

Is there a way to update a line written to the console that works with output redirection?

For example, using any of the below approaches, I can refresh the line Processing xx of yyyy on the screen as records are processed. If the application aborts before processing is complete, the Processing line will reflect the last line successfully processed (so long as the application doesn't die will updating that line). When Console.Out is redirected, I'd like the output file to similarly be updated in real time.

Approaches That Don't Work

Carriage Return Method

With the carriage return method, both old and new line are written to the output file:

Console.Write("Processed 1 of 10");
Console.Write("\rProcessed 2 of 10");

Results in:

Processed 1 of 10Processed 2 of 10

Cursor Position Manipulation

Manipulating cursor position crashes the app:

Console.Write("Processed 1 of 10");
Console.SetCursorPosition(Console.CursorLeft - 7, Console.CursorTop);
Console.Write("2 of 10");

Causes:

Unhandled Exception: System.ArgumentOutOfRangeException: The value must 
be great er than or equal to zero and less than the console's buffer size
in that dimension.
Parameter name: left
Actual value was -7.

Backspacing

The backspace method outputs both lines to the file along with the backspace characters:

Console.WriteLine("Processed 1 of 10");
Console.Write("\b\b\b\b\b2 of 10");

Produces:

Processed 1 of 10
{5 backspace characters}2 of 10
Community
  • 1
  • 1
Ben Gribaudo
  • 5,057
  • 1
  • 40
  • 75
  • 1
    What do you expect to see in the output file then? One line that says "Processed 10 of 10"? – Lars Kristensen Sep 22 '15 at 13:04
  • 1
    If the old line is already written to the file, how would you expect this to work?? You're asking for output redirection to go back and conditionally remove data from the file. That's not going to happen. – hometoast Sep 22 '15 at 13:04
  • You need to specify what you would consider a "working" solution. Please give an example of the output you would accept as OK. It isn't enough just to say "this is not what I want", you need to say what you want. – Lasse V. Karlsen Sep 22 '15 at 13:17
  • @LasseV.Karlsen -- example added. – Ben Gribaudo Sep 23 '15 at 20:10
  • Let me clarify what I meant. I meant that you need to explain what the contents of the file should be after running the program. The same program that would update the text on-screen as it processes files, if you redirect that to a file, what should the contents of that file be after the program has executed? – Lasse V. Karlsen Sep 24 '15 at 09:11
  • @LasseV.Karlsen, the file should contain exactly what the screen would show. (e.g. `Processed xx of yyyy`, where `xx` is the number of the most recently processed row.) – Ben Gribaudo Sep 24 '15 at 11:46

1 Answers1

2

That cannot possible work the way you expect it to. Even the concept of "current line" is at best blurred when output is redirected.

  • The console methods / properties that alter the cursor position on the screen are meaningless when the output is redirected.

  • The redirected output is sequential, so once something has been written to the output file, it is there.

You should consider a different strategy, i.e. changing how and what you write if you detect that the output is redirected. Use Console.IsOutputRedirected and/or Console.IsErrorRedirected, respectively.

For example:

 if (Console.IsOutputRedirected) {
    // Simply output every "line" as it comes.
    Console.WriteLine(text);
 } else {
    // Overwrite the "current line" with the next, if any.
    Console.Write(text);
    Console.SetCursporPosition(0, Cursor.CursorTop);
 }
Christian.K
  • 47,778
  • 10
  • 99
  • 143