为什么 RedirectStandardOutput 没有必要的 ANSI 代码?

发布于 2024-08-15 20:02:40 字数 546 浏览 6 评论 0原文

好的,这是我制作的一个简单的控制台应用程序,用于测试 Process.StartInfoRedirectStandardOutput

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

下面是应用结果。

控制台应用程序的结果。

因此我们可以看到颜色在控制台。

但是,当我读取 StandardOutput.BaseStream 时,没有颜色信息,没有 ANSI 代码,什么也没有。

如何捕获重定向流上的颜色信息?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2024-08-22 20:02:40

简而言之,.NET Console 类提供给您的流是纯粹基于字符的,并且仅返回文本数据。

要获取扩展颜色信息,需要 P/Invoke Win32 API 阅读控制台输出。除其他外,这将返回 COLOR_INFO< 的数组/a> 包含每个字符的颜色属性的结构。您可能需要查看 ReadConsoleOutput pinvoke.net 页面 以获取开始了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文