【发布时间】:2011-09-19 10:50:24
【问题描述】:
使用ProcessStartInfo 和Process 我想启动一个程序(例如getdiff.exe),然后读取程序产生的所有输出。稍后我将以更具建设性的方式使用数据,现在我只想打印数据以确保其正常工作。但是,程序并没有按应有的方式终止。有人知道为什么吗?提前谢谢你。
ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe");
psi.Arguments = "DIFF";
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.WorkingDirectory = "c:\\test";
Process p = Process.Start(psi);
string read = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(p);
Console.WriteLine("Complete");
p.Close();
把程序改成这样就可以正常工作了:
ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe");
psi.Arguments = "DIFF";
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.WorkingDirectory = "c:\\test";
Process p = Process.Start(psi);
StreamReader read = p.StandardOutput;
while (read.Peek() >= 0)
Console.WriteLine(read.ReadLine());
Console.WriteLine("Complete");
p.WaitForExit();
p.Close();
【问题讨论】:
-
它的哪一部分没有按应有的方式终止?您的应用程序,还是被调用的进程?
-
为什么要终止?它是控制台应用程序吗?还有什么?
-
这是一个更大程序中的函数。但是当单独运行时,程序永远不会终止。所以它不健康。
标签: c# process processstartinfo