不调用Process.Exited事件

时间:2009-11-20 07:21:41

标签: c# command-line process

我有以下代码片段来调用命令行:

p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = "/C " + "type " + “[abc].pdf”;

psi.UseShellExecute = false;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;

p.StartInfo = psi;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);
p.Start();   
p.WaitForExit();

奇怪的是,当[abc]是一个小的pdf文件(8kb)时,p_Exited被调用。但是当它是一个大的pdf文件(120kb)时,它永远不会被调用。有线索吗?

谢谢,

1 个答案:

答案 0 :(得分:2)

您需要在重定向标准输出时使用输出流:

p.Start();   
p.StandardOutput.ReadToEnd();
p.WaitForExit();