Why does setting up a console buffer raise an invalid descriptor exception when redirecting output?

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?

+4
source share
1 answer

File redirection

test1.exe > file.txt

, () , .

" /", , "". .

. ​​ , () . (. Console.WindowWidth Console.WindowHeight), - (. Console.BufferWidth Console.BufferHeight). , " " "" , , .

Console.BufferWidth = 240;

( ), (). , , Console.BufferWidth . IOException ( ).

MSDN Console :

, , , , , , . , System.IO.IOException, . IsOutputRedirected, IsInputRedirected IsErrorRedirected, , , System.IO.IOException.

+6

Source: https://habr.com/ru/post/1569780/


All Articles