运行外部应用程序的.NET Windows服务
问题描述:
我正在编写一个windows服务,它将用于监视一些其他服务并在服务关闭时运行外部应用程序。运行外部应用程序的.NET Windows服务
我已经得到了大部分工作,Windows服务运行,代码监视其他服务工程,但我遇到问题,当出现故障时运行外部应用程序。
外部应用程序仅仅是一个控制台应用程序这一串命令行开关的,但我不是100%肯定,如果开关设置正确或什么,所以我最好一)喜欢看什么样的命令正在执行并且b)查看来自它的任何输出消息。
我有这样的代码:
Process p = new Process();
p.StartInfo.FileName = "C:\MyExternalApp.exe";
p.StartInfo.Arguments = "stop";
p.Start();
p.WaitForExit();
因此,通过和我没有任何异常抛出,但我不知道,也不如果外部应用程序没有工作,我可以运行所有看到它的任何消息。
如何查看应用程序的输出?
答
Process类具有StandardOutput属性 - 它是表示过程输出的流。
从MSDN文档:
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();
你可能想的过程开始前将其StreamReader的?没有? – stephbu 2008-11-19 06:16:37