Reputation: 25097
I'm writing a windows service which will be used to monitor some other services and runs an external application if the service goes down.
I've got most of it working, the windows service runs, the code monitoring the other services works a treat but I'm having problems running the external app when there's a failure.
The external app is just a console app which a bunch of command line switches, but I'm not 100% sure if the switches are set right or what so I'd a) like to see what the command being executed is and b) see any output messages from it.
I've got this code:
Process p = new Process();
p.StartInfo.FileName = "C:\MyExternalApp.exe";
p.StartInfo.Arguments = "stop";
p.Start();
p.WaitForExit();
So that all runs through and I don't have any exceptions thrown, but I don't know if the external app didn't work nor can I see any messages from it.
How do I view the output of the app?
Upvotes: 0
Views: 643
Reputation: 2738
The Process class has a StandardOutput property - it's a stream representing the output of the process.
From the MSDN docs:
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("Process_StandardOutput_Sample.exe" );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
myProcess.Close();
Upvotes: 1