C# 如何等待批处理文件完成
我正在做一个程序,我需要启动cmd,然后启动一个批处理文件。问题是我使用的是C# 如何等待批处理文件完成,c#,windows,batch-file,cmd,wait,C#,Windows,Batch File,Cmd,Wait,我正在做一个程序,我需要启动cmd,然后启动一个批处理文件。问题是我使用的是MyProcess.WaithForexit()并且我认为它不会等到批处理文件处理完成。它只是等待cmd关闭。到目前为止,我的代码是: System.Diagnostics.ProcessStartInfo ProcStartInfo = new System.Diagnostics.ProcessStartInfo("cmd"); ProcStartInfo.RedirectStandardOutput
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();
我需要等待批处理文件完成。我该怎么做 start命令的参数可以使它等待启动的程序完成。按如下所示编辑参数以传递“/wait”:
ProcStartInfo.Arguments = "/c start /wait batch.bat ";
我还建议您希望批处理文件退出cmd环境,因此在批处理结束时放置一个“exit”
@echo off
rem Do processing
exit
这应该可以达到预期的效果。这实际上对我来说效果很好:
System.Diagnostics.Process.Start("myBatFile.bat").WaitForExit();
正如米尔顿所说,在批处理文件末尾添加“exit”很可能是个好主意
干杯开始
启动批次并返回。删除start
。在批处理文件末尾添加“exit”是至关重要的。这应该是公认的答案tbh,对于我正在寻找的+1来说非常简单和有效