Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the RedirectStandardOutput does not have the necessary ANSI codes?

Ok here's a simple Console Application I made to test the RedirectStandardOutput of the Process.StartInfo.

    foreach (c In [Enum].GetValues(GetType(ConsoleColor))
    {
        Console.ForegroundColor = c
        Console.WriteLine("Test")
    }

And below is the application result.

Result of the Console Application.

So as we can see the colors show beautifully on the console.

However, when I read the StandardOutput.BaseStream there's no color information, no ANSI codes, no nothing.

How do I capture the color information on the redirected stream?

like image 587
Paulo Santos Avatar asked Nov 15 '22 13:11

Paulo Santos


1 Answers

The short answer is that the streams as given to you by the .NET Console class are purely character-based and return only textual data.

To get the extended color info, it would be necessary to P/Invoke the Win32 API ReadConsoleOutput. This will return, among other things, an array of COLOR_INFO structs containing the color attributes for each character. You might want to look at the ReadConsoleOutput pinvoke.net page to get started.

like image 162
bobbymcr Avatar answered May 16 '23 07:05

bobbymcr