开发者

c# call batch with error-redirect and exe over cmd via process

开发者 https://www.devze.com 2023-04-06 23:11 出处:网络
I use the following lines to create a process: process.StartInfo.FileName = \"cmd.exe\"; process.StartInfo.WorkingDirectory = buildProject.DirectoryName;

I use the following lines to create a process:

process.StartInfo.FileName = "cmd.exe";
process.StartInfo.WorkingDirectory = buildProject.DirectoryName;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;

Then I start the process and pass some commands to it:

process.Start();

StreamWriter stream = process.StandardInput;

// call batch
stream.WriteLine(@"call ""test.bat""");

// call exe
stream.WriteLine("echo msbuild!");

// exit-command
stream.WriteLine("exit");

All works fine until the batchfile contains two nul-redirects:

@call :Jump_1
@call :Jump_2

@goto end


@REM -----------------------------------------------------------------------
:Jump_1
@echo Jump_1
@call :Jump_1_1 1>nul 2>&1                   REM critical!!!
@exit /B 0

:Jump_1_1
@echo Jump_1_1
@exit /B 0


@REM -----------------------------------------------------------------------
:Jump_2
@echo Jump_2
@call :Jump_2_1 1>nul 2>&1                   REM critical!!!
@exit /B 0

:Jump_2_1
@ech开发者_如何学运维o Jump_2_1
@exit /B 0


@REM -----------------------------------------------------------------------
@echo End
:end

In this case the process stops immediatly. StandardOut shows this:

Microsoft Windows XP [Version 5.1.2600]

(C) Copyright 1985-2001 Microsoft Corp.

C:\Entwicklung\ProcessTest\ProcessTest\bin\Debug>call "test.bat"

Jump_1

Jump_2

It only crashs if there are two Redirects if I remove one all is fine.

Because the original script is delivered with VS2010 I cannot change the script.


Edit: I updated the question with more details.


Is it the redirection to nul that fails, or is it the redirection from stderror to stdout (2>&1)?

Your code doesn't set RedirectStandardError to true, you should set this to true and it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消