我需要在程序后台执行VS命令行,
比如执行如下命令: Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
//配置VS命令环境
string cmd = "%comspec% /k "+"\"D:\\Program Files\\Microsoft Visual Studio 9\\VC\\vcvarsall.bat\""+" x86";
p.StandardInput.WriteLine(cmd);
p.StandardInput.WriteLine("exit");
MessageBox.Show(p.StandardOutput.ReadToEnd());这样当执行到最后一步时就会死掉,因为执行p.StandardInput.WriteLine(cmd)时会花费一定的时间,它会忽略掉p.StandardInput.WriteLine("exit"),因此在执行方法p.StandardOutput.ReadToEnd()时就会卡死,这个怎么解决呢
比如执行如下命令: Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
//配置VS命令环境
string cmd = "%comspec% /k "+"\"D:\\Program Files\\Microsoft Visual Studio 9\\VC\\vcvarsall.bat\""+" x86";
p.StandardInput.WriteLine(cmd);
p.StandardInput.WriteLine("exit");
MessageBox.Show(p.StandardOutput.ReadToEnd());这样当执行到最后一步时就会死掉,因为执行p.StandardInput.WriteLine(cmd)时会花费一定的时间,它会忽略掉p.StandardInput.WriteLine("exit"),因此在执行方法p.StandardOutput.ReadToEnd()时就会卡死,这个怎么解决呢
p.WaitForExit();
试试呢。