简体   繁体   English

获取运行过程C#或Powershell Windows的控制台输出

[英]Get console output of runnning process c# or powershell windows

Is it possible to get the console output of a running process? 是否可以获取正在运行的进程的控制台输出? I know I can get a list of all running processes like this: 我知道我可以获得所有正在运行的进程的列表,如下所示:

Process.GetProcesses();

But how can I tap into the standard output and pipe it to a file or to a different console app. 但是我如何才能进入标准输出并将其通过管道传输到文件或其他控制台应用程序。

The presuppose is to create a debugging\\ tracing tool to an existing application that I cannot change. 前提是为我无法更改的现有应用程序创建调试\\跟踪工具。 The app fires a specific process and does not set the RedirectStandardOutput property so I have no way of diganosting the output of that child process. 该应用会触发一个特定的进程,并且不会设置RedirectStandardOutput属性,因此我无法降低该子进程的输出。

What you're asking for isn't really possible in C#. 您所要求的在C#中实际上是不可能的。 If you drop down a layer to native C++, it's possible (though still relatively difficult) to accomplish this by using the Windows API to implement API Hooking as described in this answer . 如果将层放到本机C ++中,则可以(尽管仍然相对困难)通过使用Windows API来实现API Hooking (如本答案所述)来完成此操作。

Before going down this road, pay attention to the rest of the discussion in that answer. 在走这条路之前,请注意该答案中其余的讨论。 The vast majority of the time, this isn't want you want to do. 大多数时候,这不是您不想做的。 As mentioned in that discussion, a program owns its own stdout and you don't change carpets in someone else's house . 就像在讨论中提到的那样,一个程序拥有自己的标准输出, 您不必在别人家中更换地毯 Also anticipate what should happen if two programs try to do this at once . 还要预料如果两个程序尝试一次执行该操作会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM