Search code examples
c#batch-fileerror-handlingexit-code

How get string message with exit code from c# file to provide .bat file that runs it


I'm running a c# .exe file like this:

runas /user:company\username "D:\company\Batch\dir\program.bat

so that's running

start /d "D:\company\Batch\dir" program.exe > program.txt

I'm trying to get error info to show up in program.txt.

I've been looking at exit codes for c#, but it doesn't look like I can give it specific error info. error codes. Does anyone know how I can give the error code specific error info from the c# program? Like I could give TimeoutException, Exception, CommunicationException, service response error message/code, db connection failure, etc. I'd hate to provide a generic failure, when I know info on the failure to give.


Solution

  • Looks like console.writeline's are showing in command window as follows.

    We open a separate windows command window, and run it like this: (where command window is logged in as other user)

    C:\Users\stillShowsOriginalUsernameHere>D:\company\Batch\dir\program.exe

    Then it shows error messages in command window. So when I schedule it with the scheduler, it will capture the output as error info.