Reading Windows Command Prompt STDOUT

I have a command line application that runs on a Windows server. The command line remains open when the program is running, and log messages are displayed in the command window when the program is running.

I need to read the messages that appear on the command line when the program starts, and then run certain commands if a certain set of words appears in the messages.

What is the easiest way to do this on a windows machine? (without changing the application)

0
source share
1 answer

Reading these two posts will give you a solution:

, ( ) ( #) - , , .

:

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");
}
+3

All Articles