阅读Windows命令提示符STDOUT [英] Read Windows Command Prompt STDOUT
问题描述
我有一个在Windows服务器上运行的命令行应用程序。程序运行时命令提示符保持打开状态,日志消息作为程序功能输出到命令提示符窗口。
I have a command line application that runs on a windows server. The command prompt remains open when the program is running, and log messages are output to the command prompt window as the program functions.
我需要阅读出现的消息在程序运行时,在命令提示符处显示,然后在消息中出现一组特定的单词时运行特定的命令。
My need is to read the messages that appear on the command prompt as the program runs, and then run particular commands if a specific set of words appear in the messages.
在Windows上执行此操作的最简单方法是什么机? (无需修改应用程序)
What's the easiest way to do this on a windows machine? (without modifying the app)
推荐答案
阅读这两篇文章将为您提供解决方案:
Reading those two posts will give you the solution:
- ProcessStartInfo
- Capturing console output.
这个想法是从新应用中运行您的应用(而不是对其进行修改)(
The idea is to to run your app (not modifying it) from your new app (written in C#) and redirect its input-output here, reading and writing as you please.
一个例子可能是:
Process proc;
void RunApp()
{
proc = new Process();
proc.StartInfo.FileName = "your_app.exe";
proc.StartInfo.Arguments = ""; // If needed
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.OutputDataReceived += new DataReceivedEventHandler(InterProcOutputHandler);
proc.Start();
proc.WaitForExit();
}
void InterProcOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
// Read data here
...
// Send command if necessary
proc.StandardInput.WriteLine("your_command");
}
这篇关于阅读Windows命令提示符STDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!