且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中读取控制台应用程序输出

更新时间:2023-02-19 19:43:09

如果你无法识别应用程序,那么你真的没有太多可以做的 - 如果它被第三个打开用户请求的派对app然后几乎找到它的唯一方法是读取活动的Windows列表并查找更改。但是......会找到用户打开的每个应用程序,包括Solitaire,Notepad,IE ......,所以隔离你想要的应用程序可能并不简单。



我不确定你是否可以获取控制台应用程序的文本输出,除非你的应用程序是创建过程的应用程序 - 我很确定你需要将输出流转移到你的应用程序通过 Process.StandardOutput [ ^进程对象的属性。



您认为这可能是一个解决方案,您打算做什么?
If you can't identify the app, then there really isn't a lot you can do - and if it's being opened by a third party app at the users request then pretty much the only way to find it is to read the active windows list and look for changes. But...that will find every application the user opens, including Solitaire, Notepad, IE..., so isolating the one you want probably won't be simple.

And I'm not sure that you can "get at" the text output of a console app anyway unless your application is the one creating the process - I'm pretty sure that you would need to divert the output stream to you app via the Process.StandardOutput[^] property of the Process object.

What are you trying to do that you think this might be a solution?


这是通过用于启动应用程序的类 System.Diagnostics.Process 完成的。由于需要重定向(见下文),您需要使用 System.Diagnostics.ProcessStartInfo 使用 Start 方法:

https:// msdn.microsoft.com/en-us/library/system.diagnostics.process%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/0w4h05yb%28v= vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v = vs.110).aspx [ ^ ]。



您需要重新定向 StandardOutput 流(以防万一, StandardError ),拿起你开始的应用程序的输出。



重定向代码示例可以在这里找到:https://msdn.microsoft.com/en-us/library/system.diagnostics .process.standardoutput%28v = vs.110%29.aspx [ ^ ]。



现在,给你一个大警告,以防万一:不要重复在这个论坛上发现的一个愚蠢的错误:不要使用CMD.EXE。这完全没有意义。在您的示例中,构造函数调用 ProcessStartInfo(string,string)应直接使用ping和192.168.1.1作为第一个和第二个参数:

https://msdn.microsoft.com/en-us/library /b5zzxthe(v=vs.110).aspx [ ^ ]。



-SA
This is done with the class System.Diagnostics.Process used to start your application. As redirection will be needed (see below), you need to use the Start method using System.Diagnostics.ProcessStartInfo:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/0w4h05yb%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx[^].

You need to re-direct the StandardOutput stream (and, just in case, StandardError), to pick up the output of the application you start.

The redirection code sample can be found here: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.110%29.aspx[^].

Now, a big warning for you, just in case: don't repeat one of the stupid mistakes found on this forum: don't use CMD.EXE. It's totally pointless. In your example, your constructor call ProcessStartInfo(string, string) should directly use "ping" and "192.168.1.1" for first and second parameters:
https://msdn.microsoft.com/en-us/library/b5zzxthe(v=vs.110).aspx[^].

—SA