0

I'd like to execute a batch file without showing terminal window and I need to get the standard output contents. Here is the batch file:

timeout /T 5
exit 0

Below is my partial C# code:

static void Main(string[] args)
{
    bool ShowTerminal = true;

    Process proc = new Process();
    proc.StartInfo.FileName = "cmd.exe";
    string ExecContent = "\"" + args[0] + "\"";
    proc.StartInfo.Arguments = "/c " + ExecContent;
    proc.StartInfo.UseShellExecute = ShowTerminal;
    proc.StartInfo.RedirectStandardError = !ShowTerminal;
    proc.StartInfo.RedirectStandardInput = !ShowTerminal;
    proc.StartInfo.RedirectStandardOutput = !ShowTerminal;
    proc.Start();
    proc.WaitForExit();
}

I found that variable ShowTerminal set true, then everything is going well except I can not get standard output contents

But if variable ShowTerminal set false, timeout command will be skipped

Is there any solution for this problem? Thanks!

4
  • 1
    Why not redirect the STDOUT to a file like cmd.exe > output.txt Commented Jul 29, 2014 at 3:15
  • There are hundred of batch file need to execute parallel, redirection may not a good way... Commented Jul 29, 2014 at 3:23
  • 1
    Instead of proc.StartInfo.UseShellExecute = ShowTerminal, have you tried proc.StartInfo.CreateNoWindow = !ShowTerminal? Commented Jul 29, 2014 at 5:21
  • Ignore proc.StartInfo.UseShellExecute = ShowTerminal will cause an exception: System.InvalidOperationException :( Commented Jul 29, 2014 at 6:34

1 Answer 1

0

If you specify "redirect" options you should also provide their redirection streams (STDIN, STDOUT) and ensure they are being emptied/read/processed as CMD would expect.

Chances are good that 'timeout' is failing to detect any STDIN/STDOUT and therefore performs a noop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.