2015-06-06 99 views
3

我正在做一個程序,我需要啓動cmd並啓動一個批處理文件。問題是我使用MyProcess.WaithForexit();,我認爲它不會等到批處理文件處理完成。它只是等到cmd關閉。我的代碼到目前爲止:如何等到我的批處理文件完成

System.Diagnostics.ProcessStartInfo ProcStartInfo = 
    new System.Diagnostics.ProcessStartInfo("cmd"); 
    ProcStartInfo.RedirectStandardOutput = true; 
    ProcStartInfo.UseShellExecute = false; 
    ProcStartInfo.CreateNoWindow = false; 
    ProcStartInfo.RedirectStandardError = true; 
    System.Diagnostics.Process MyProcess = new System.Diagnostics.Process(); 
    ProcStartInfo.Arguments = "/c start batch.bat "; 
    MyProcess.StartInfo = ProcStartInfo; 
    MyProcess.Start(); 
    MyProcess.WaitForExit(); 

我需要等到批處理文件完成。我怎麼做?

+6

'start'啓動批處理並返回。刪除'開始'。 – CodeCaster

回答

2

啓動命令的參數可以使其啓動程序完成WAIT。編輯參數,如下所示通過「/等待」:

ProcStartInfo.Arguments = "/c start /wait batch.bat "; 

我也建議你希望你的批處理文件,退出CMD envirionment所以放置一個「退出」在批處理結束。

@echo off 
rem Do processing 
exit 

這應該達到預期的行爲。

+2

在批處理文件的末尾添加「退出」至關重要。 – Sarah

相關問題