不通过 stdout 写入控制台窗口
如何直接写入控制台,而不用担心 c# 中的 stdout?我正在升级一些旧程序,它的标准输出重定向到文件中(因为输出很重要),我需要以某种方式直接写入控制台,并且文本不会出现在标准输出中。有可能吗(不使用WinAPI)?
编辑:我知道可以写入 stderr,但是是否可以在控制台上设置 stderr 的光标位置?
how can I write DIRECTLY into console, without bothering with stdout in c#? I'm upgrading some old program, which has it's stdout redirected into file (because the output matters), and I need somehow to write directly to console, and that the text won't appear in stdout. Is it possible (without using WinAPI)?
EDIT: I'm aware of the possibility to write to stderr, althrough, is it possible to set cursor position for stderr on console?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以写入
Console.Error
,尽管它可以重定向,但它与 stdout 是分开的。编辑:假设stderr尚未重定向,请参阅
Console.CursorTop
和Console.CursorLeft
。Console
类上还有各种其他成员< /a> 您可能会发现有用。要直接回答您的问题,请使用 Win32
WriteConsole
功能。据我所知,.NET 框架没有直接写入控制台窗口的方法。You could write to
Console.Error
, which, although it can be redirected, is separate from stdout.Edit: Assuming that stderr has not been redirected, see
Console.CursorTop
andConsole.CursorLeft
. There's various other members on theConsole
class that you might find useful.To answer your question directly, use the Win32
WriteConsole
function. As far as I can see, the .NET framework doesn't have methods for writing directly to the console window.如果我运行你的程序并重定向它的 StandardOutput 和 StandardError,当没有控制台时你期望你的写入会发生什么?
出于这个原因,答案可能是“你不能”(除非使用疯狂的黑客技术,这可能涉及你说你不想使用的 Windows API)。
将控制台窗口视为一个 UI 元素,它允许用户查看程序的 stdout/stderr(并提供 stdin)。它实际上并不存在任何其他目的。
If I run your program and redirect its StandardOutput and StandardError, what do you expect to happen to your writes when there is no console?
For this reason, the answer is likely “you can’t” (except perhaps by using crazy hacks, which probably involve Windows API which you said you didn’t want to use).
Think of the console window as nothing more than a UI element that allows the user to view the stdout/stderr of your program (and to provide stdin). It doesn’t really exist for any other purpose.
我实际上最终实现了低级别 WriteConsoleOutput 不久前,作为我编写的基于终端的俄罗斯方块实现的一部分,因为使用
Console.BackgroundColor
和Console.Write
进行彩色控制台输出太过分了全屏刷新速度慢。进行原始缓冲区输出快快得多。请注意,这会将文本写入屏幕上的某个位置(例如 5,10),但不会自行更新或跟踪光标位置 - 使用此方法写入将更新文本缓冲区并显示输出,但光标不会移动。您需要使用其他方法手动移动和跟踪控制台光标,这应该不会太困难。
这是我的代码:
主要互操作类:
人物属性:
测试代码:
I actually ended up implementing the low level WriteConsoleOutput some time ago as part of a terminal based Tetris implementation I wrote, since doing coloured console output with
Console.BackgroundColor
andConsole.Write
, is far too slow for full screen refreshes. Doing the raw buffer output is much faster.Note, this writes text to a location on the screen (eg 5,10), but doesn't update or keep track of the cursor position on it's own - writing with this method will update the text buffer and display the output, but the cursor won't move. You'll need to move and keep track of the console cursor manually with some other method, which shouldn't be too difficult.
Here's my code:
Main interop class:
Character attributes:
Test code: