Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中启动批处理文件,然后等待其退出,然后继续_C#_.net_Batch File - Fatal编程技术网

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

在C#中启动批处理文件,然后等待其退出,然后继续,c#,.net,batch-file,C#,.net,Batch File,问题在于WaitForExit不会等到批处理文件退出。它几乎马上就回来了 我将按如下方式启动批处理文件: ProcessStartInfo startInfo = new ProcessStartInfo(batchFile); startInfo.UseShellExecute = true; startInfo.Arguments = arguments; using (Process p =

问题在于WaitForExit不会等到批处理文件退出。它几乎马上就回来了

我将按如下方式启动批处理文件:

            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()不起作用,请尝试执行同样不起作用的命令。该批处理文件必须是唯一的。批处理文件调用其他进程,但要等到它们全部退出后才能返回。