【问题标题】:Starting a batch file in C# and then waiting for it to exit before continuing在 C# 中启动一个批处理文件,然后等待它退出再继续
【发布时间】:2013-10-08 19:01:15
【问题描述】:

问题是 WaitForExit 没有等到批处理文件退出。它几乎马上就回来了。

我正在按如下方式启动我的批处理文件:

            ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
            startInfo.UseShellExecute = true;
            startInfo.Arguments = arguments;

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

我尝试了使用和不使用UseShellExecute

【问题讨论】:

  • This answer 可能会有所帮助。
  • 如果 WaitForExit() 不起作用,请尝试关注 this
  • 那也没用。这个批处理文件一定是独一无二的。批处理文件会调用其他进程,但会等到它们全部退出后再返回。

标签: c# .net batch-file


【解决方案1】:

您可以尝试使用“/c yourbatchfile”作为命令行参数来运行 cmd。

【讨论】:

    【解决方案2】:

    看来你可以重定向StdOut并阅读它直到它关闭。

    这个想法来自this similar question

    调整你的 sn-p,那就是:

    ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
    //startInfo.UseShellExecute = true;
    startInfo.Arguments = arguments;
    startInfo.RedirectStandardOutput = true;
    
    Process p = Process.Start(startInfo);
    String output = proc.StandardOutput.ReadToEnd();
    

    【讨论】:

      猜你喜欢
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多