Issues with Console APIs when stdout is redirected to a file [Ravi Krishnaswamy]

When you redirect stdout of your consoleapp to a file, you can expect that a number of Console APIs won't work quite right. For ex, MoveBufferArea is an obvious candidate of something that can't be supported. In this situation you will get an IOException saying that the handle is invalid or something like that. We are throwing here not specifically because the output is redirected but mostly because we can't get a console handle.

Recently, we changed the behavior of some of the Console APIs such as BackgroundColor/ForegroundColor/ResetColor to be essentially nop if we can't get hold of a console handle. Seems like a reasonable thing to do.

Unfortunately, we didn't do a full review of all the Console APIs and see whether we should extend the nop behavior to other benign cadidates such as Console.SetConsoleCursorPosition, Console.Clear etc. So, some of Console APIs are still broken for redirection scenarios in Whidbey.

What this means is, to enable stdout redirection of you app, you essentially need to anticipate and catch the IOException around all Console APIs. This option is less than enterprising but would work. Alternatively, you could P/Invoke and call GetStdHandle(Win32Native.STD_OUTPUT_HANDLE) and see it is not returning INVALID_HANDLE_VALUE. Again, this is something that should be handled internally by the Console APIs where appropriate.

Ravi Krishnaswamy
BCL