tags:

views:

108

answers:

1

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?

+1  A: 

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();
Jeff Donnici
You probably want to attach the streamreader before the process is started? no?
stephbu
Probably... that code was from the linked MSDN page. I think what happens in this case is that the StreamReader is attached right away because execution won't stop at the Start() call (unless the other properties are set for it to).
Jeff Donnici