在C#中启动批处理文件,等待其退出后再继续。

3
问题在于WaitForExit方法并不会等待批处理文件退出,它几乎马上就返回了。
我是这样启动批处理文件的:
            ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
            startInfo.UseShellExecute = true;
            startInfo.Arguments = arguments;

            using (Process p = Process.Start(startInfo))
            {
                p.WaitForExit();
            }

我尝试过使用和不使用UseShellExecute


2
这个答案可能会有所帮助:https://dev59.com/t1rUa4cB1Zd3GeqPjGCy#7101534 - Michael Todd
如果 WaitForExit() 不起作用,请尝试遵循此链接:http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx - Tinwor
那也不起作用。这个批处理文件肯定有些独特的东西。该批处理文件调用其他进程,但在返回之前并不等待它们全部退出。 - 101010
2个回答

1
你可以尝试在命令行参数中使用"/c yourbatchfile"来运行cmd。

0

看起来你可以通过重定向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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接