C#:调用批处理文件并跳过“按任意键继续”

时间:2017-03-13 19:26:52

标签: c# winforms batch-file

我在C#程序中调用批处理文件。但批处理文件有“暂停”,所以我得到“按任意键继续”窗口。我无法编辑批处理文件。有没有办法跳过这个?我正在使用下面的代码(found in this SO answer)并玩弄它 - 但没有运气。

提前谢谢大家。

int exitCode;
string command = "test.bat"
ProcessStartInfo processInfo;
Process process;
string path = @"C:\test\test";

processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.WorkingDirectory = path;
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;

process = Process.Start(processInfo);
process.WaitForExit();

string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

exitCode = process.ExitCode;

MessageBox.Show("output>>" + (String.IsNullOrEmpty(output) ? "(press any key to continue...)" : output));
MessageBox.Show("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
MessageBox.Show("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
process.Close();

1 个答案:

答案 0 :(得分:0)

尝试

processInfo = new ProcessStartInfo("cmd.exe", "/c \"break|" + command+"\"");