是否可以在 .net 中捕获/重定向我未启动的进程的标准输出?
如果我知道进程标识符,.net 中是否有某种机制(或作为最后手段的 pinvoke)来捕获/重定向我未启动的进程的标准输出?
If I know the process identifier, is there some mechanism in .net (or pinvoke as last resort) to capture/redirect the standard output of a process that I did not launch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以AttachConsole您的进程到控制台另一个进程,然后使用常用的控制台 I/O 函数对其进行读写。
请记住使用 FreeConsole 与首先处理当前控制台。
You could AttachConsole your process to the console of the other process, and then use the usual console I/O functions to read and write from it.
Remember to use FreeConsole to detach from the process' current console first.
据我所知,您无法在 .NET 中执行此操作,您可以重定向仅通过 ProcessStartInfo 类。
另外据我所知,在进程启动后根本无法在外部重定向 I/O,只有进程本身可以做到这一点。
您可以通过 附加控制台功能。
As far as I know you can't do this in .NET, you can redirect outputs for processes you start only via ProcessStartInfo Class.
Also as far as I know you cannot redirect I/O externally at all after a process has started, only the process itself can do it.
You can attach the calling process to the console of the specified process via AttachConsole Function.
编辑:看起来它确实不起作用,正如本线程中的另一个答案所指出的那样。
我没有亲自尝试过,但自从答案被接受以来,这个 作品。
EDIT: Looks like it indeed does not work, as is being pointed out by another answer in this thread.
I have not personally tried it, but it seems, since the answer was accepted, that this works.