Executing Batch files from C#

Addy7

Executing Batch files from C#

by Addy7 » Mon, 04 Aug 2008 08:25:54

Hi

I am executing a batch program from c# and It works fine as in code shown below.

But I am not able catch any exceptions thrown while executing batch file.

Example the getfiles.bat has command >>open ftp.###.com

If there is a dos exception like bad ftp url the exception is not transmitted back to C# app.

I just wanted to know if it possible to transmit the error back to C# app. I want to check if the batch file executed sucessfully.

Thanks in Advance...

/*********88

try

{

System.Diagnostics.Process proc;

proc = System.Diagnostics.Process.Start("C:\\getfiles.bat");

proc.WaitForExit();

return true;

}

catch (Exception ex)

{

UploadException.AddExceptionMessage (ex.Message, false, true);

return false;

}



ahmedilya

Executing Batch files from C#

by ahmedilya » Wed, 06 Aug 2008 09:26:55

the process is an external process - a class which executes an external process and doesnt care about any runtime errors from it therefore you won't be able to catch any exceptions. I'm not sure of a work around but I DO know that you can redirect the input/output/error stream from the PSI - ProcessStartInfo.

take a look at this for more information and hope it steers you in the right direction:

http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx