c# - Exit code from a batch file converted to an exe is not returning 0 when called by Process.Start() -
c# - Exit code from a batch file converted to an exe is not returning 0 when called by Process.Start() -
summary: c# app supposed phone call gpg.exe encrypt file before sending out. in testing found computer configuration gpg consistently fails.
the goal have create wrapper batch file calls gpg prints command line args output file, , have our app phone call this.
the c# code calling command:
public static int startprocess(string command, string arguments, int waitforprocess) { processstartinfo startinfo = null; if (string.isnullorempty(arguments)) startinfo = new processstartinfo(command); else startinfo = new processstartinfo(command, arguments); startinfo.createnowindow = true; startinfo.errordialog = false; startinfo.useshellexecute = false; process p = new process(); p.startinfo = startinfo; p.start();
note have no command on filename scheme expecting production code: expecting command c:\program files\gnupg\gpg.
i renamed real gpg.exe gpg2.exe, , created batch file (ignore terrible batch code unless relevant please. it's supposed quick diagnostic rather robust solution):
@echo off echo %* > "c:\program files\gnu\gnupg\gpgoutput.txt" "c:\program files\gnu\gnupg\gpg2" %1 %2 %3 %4 %5 %6 %7 %8 %9 >> gpgoutput.txt if errorlevel 1 echo 1 >> gpgoutput.txt if errorlevel 0 echo 0 >> gpgoutput.txt if errorlevel 2 echo 2 >> gpgoutput.txt exit /b 0
turns out p.start() throw error , isn't able find batch file. so, downloaded bat_to_exe_converter convert bat file exe format. saved file gpg.exe
process flow should c# app calls 'gpg.exe' > calls gpg2.exe , logs variables/exit code.
this code runs fine command prompt when called c# app, exit code 9211 , can see gpgoutput.txt file not created, implying doesn't run 'gpg.exe'
why doesn't above code executed correctly c# application? thought must related either permissions or directory somehow.
edit: have tried manually setting command c:\program files\gnupg\gpg.bat , code works fine. issue seems caused a) error if looking c:\program files\gnupg\gpg , file saved gpg.bat "the scheme cannot find file specified" b) if convert batch gpg.exe, scheme can find file, gives exit code ~9200. fault of exe converter?
first, not utilize batch-file-converted-to-exe. that's going create things harder.
second, should able run batch file using process. processstartinfo.useshellexecute
set true
, expect "just work". barring that, should able execute command-line interpreter, "cmd.exe /c ..." "..." total command execute batch file. if still having problems there, might inquire different question.
finally, far wrapper batch file goes, obvious problem explicitly setting exit code 0 exit /b 0
. if want exit code gpg2.exe process returned, need save after running gpg2.exe (e.g. set exitcode=%errorlevel%
), , utilize value when exit batch file (e.g. exit /b %exitcode%
).
if homecoming actual exit code process batch file, c# programme should able read code fine.
(saving value ensures other statements later in batch file, impact errorlevel value, won't interfere returning right exit code.)
c# batch-file
Comments
Post a Comment