2

I'm calling a batch file in my C# program. But the batch file has that "pause" so I get the "Press any key to continue" window. I cannot edit the batch file. Is there a way to skip this? I'm using the code below(found in this SO answer) and played around with it - but without luck.

Thank you everyone in advance.

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();
Community
  • 1
  • 1
Besiktas
  • 331
  • 1
  • 6
  • 23
  • 1
    Some suggestions: 1. Try using ShellExecute, 2. Try using the switch /q with cmd – Psi Mar 13 '17 at 19:31
  • I'd personally just parse the batch file, and run each command... or there is a sendkey if you really need the dos prompt.. – Trey Mar 13 '17 at 19:32
  • @Psi tried both with no luck. – Besiktas Mar 13 '17 at 19:40
  • @Trey Not really sure what you mean. I'm really new with C#. Anyway you can provide a code? Thanks!!! – Besiktas Mar 13 '17 at 19:40
  • Parsing a batch is hard if it contains conditionals or variables. Do you have a statement called `pause` in your batch file? – Psi Mar 13 '17 at 19:41
  • @Psi yes I do. But as I mentioned above, I can't change that. – Besiktas Mar 13 '17 at 19:43
  • 2
    Ah sry, didn't see that. So then the only thing you can do is: read the .bat, remove the `pause` in memory, save the result to a temporary location and execute that one – Psi Mar 13 '17 at 19:45
  • 2
    What I mean is a batch file is just a text file, just read in the lines, and execute them like you did cmd. ..or just remove the pause, save to temp batch file locally, and run the damned thing. – Trey Mar 13 '17 at 19:46

1 Answers1

1

try with

processInfo = new ProcessStartInfo("cmd.exe", "/c \"break|" + command+"\"");
npocmaka
  • 55,367
  • 18
  • 148
  • 187