Setting Console.BufferWidth in a C # console application throws an exception when redirecting output to a file. Call the test1.exe example:
static void Main(string[] args) {
Console.BufferWidth = 240;
Console.WriteLine("output1\noutput2");
}
Standard output:
test1.exe
output1
output2
Redirection to exclude file failure:
test1.exe > file.txt
Unhandled Exception: System.IO.IOException: The handle is invalid.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.Console.SetBufferSize(Int32 width, Int32 height)
at System.Console.set_BufferWidth(Int32 value)
at test1.Program.Main(String[] args) in \\wopr\falken\test1\Program.cs:line 13
It's easy enough to ignore the use of try ... catch, but there is something I don’t understand about handles or file descriptors in general.
Why is the descriptor invalid?
source
share