1

I'm trying to execute a simple batch script from a C# program.

Here's a minimal example:

Batch file

TIMEOUT /T 30 >nul
EXIT /B 1

C# program

    static void Main(string[] args)
    {
        Process process = new Process();
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = false;
        process.StartInfo.FileName = "D:\\xxx.bat";
        process.Start();
        string output = process.StandardOutput.ReadToEnd();
        bool exit = process.WaitForExit(120000);
        Console.WriteLine(output);
    }

Output

"\r\nC:\\Users\\xxx\\bin\\Debug>TIMEOUT /T 30 \r\n\r\nWaiting for 30 seconds, press a key to continue ...\r\n\r\nC:\\Users\\xxx\\bin\\Debug>EXIT /B 1 \r\n"

I'd expect the batch script to produce no output (due to nul redirection of the timeout command) but it still does. The output contains the program path and the command text. Why is that? (Is it outputting the script trace eg. like bash -x? Google doesn't tell me how/if it could be disabled)

1 Answer 1

1

Redirecting output to nul should only have hidden the output of the redirected command.

Instead, you can add @echo off at the start of your bat file.

This will:

From "Echo", Technet

prevent all commands in a batch file (including the echo off command) from displaying on the screen.

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.