在C#中启动批处理文件,然后等待其退出,然后继续
问题在于WaitForExit不会等到批处理文件退出。它几乎马上就回来了 我将按如下方式启动批处理文件:在C#中启动批处理文件,然后等待其退出,然后继续,c#,.net,batch-file,C#,.net,Batch File,问题在于WaitForExit不会等到批处理文件退出。它几乎马上就回来了 我将按如下方式启动批处理文件: ProcessStartInfo startInfo = new ProcessStartInfo(batchFile); startInfo.UseShellExecute = true; startInfo.Arguments = arguments; using (Process p =
ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
startInfo.UseShellExecute = true;
startInfo.Arguments = arguments;
using (Process p = Process.Start(startInfo))
{
p.WaitForExit();
}
我尝试了使用和不使用
UseShellExecute执行,似乎您可以重定向StdOut
并读取它,直到它关闭
这个主意是从我的朋友那里学来的
调整您的代码片段,这将是:
ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
//startInfo.UseShellExecute = true;
startInfo.Arguments = arguments;
startInfo.RedirectStandardOutput = true;
Process p = Process.Start(startInfo);
String output = proc.StandardOutput.ReadToEnd();
您可以尝试使用“/c yourbatchfile”作为命令行参数来运行cmd。可能会有所帮助。如果WaitForExit()不起作用,请尝试执行同样不起作用的命令。该批处理文件必须是唯一的。批处理文件调用其他进程,但要等到它们全部退出后才能返回。