.NET Windows Service works with external applications

I am writing a Windows service that will be used to monitor some other services and start an external application if the service is down.

I have most of the work, the windows service is running, the code that controls other services is running, but I am having trouble starting an external application when there is a crash.

The external application is just a console application that toggles the command line bunch, but I'm not 100% sure if the switches are set correctly or that I could a) see what the command is running and b) see any output messages.

I have this code:

Process p = new Process();
p.StartInfo.FileName = "C:\MyExternalApp.exe";
p.StartInfo.Arguments = "stop";
p.Start();
p.WaitForExit();

      

So everything goes through and I have no exceptions, but I don't know if the external application is down and I don't see any messages from it.

How do I view the output of an application?

0


source to share


1 answer


The Process class has a StandardOutput which is a stream representing the result of a process.

In 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();

      

+1


source







All Articles