0

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.

What's the easiest way to do this on a windows machine? (without modifying the app)

Sev
  • 15,401
  • 9
  • 56
  • 75

1 Answers1

3

Reading those two posts will give you the solution:

  1. ProcessStartInfo
  2. 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.

An example could be:

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");
}
Community
  • 1
  • 1
Marco
  • 56,740
  • 14
  • 129
  • 152