Discussion:
Process.ExitCode() == 0
(too old to reply)
Andy
2006-02-20 17:35:29 UTC
Permalink
I am calling out to an executable using the System.Diagnostics.Process
methods and specifically attempting to trap for errors (at least trying to
learn how to trap for errors). I arranged the data to specifically error
when calling this executable.

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "intupld.exe";
process.StartInfo.Arguments = hupFile;
process.StartInfo.WorkingDirectory = hencePath;
process.Start();
if (process.ExitCode != 0)
{

}

However: my .ExitCode is ALWAYS 0, regardless of if an error occured using
this exectable. When I run the executable from a dos prompt: I am able to
see the errors using the same file.

How would I actually be able to trap and find the errors that have occured
while running this executable?

Thanks
Andy
Peter Bromberg [C# MVP]
2006-02-20 18:20:26 UTC
Permalink
Andy,
It is up to you to set the exit code inside the target executable's code. If
you do not,
it will always be zero.
You might also want to consider using the WaitForExit method as well.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
Post by Andy
I am calling out to an executable using the System.Diagnostics.Process
methods and specifically attempting to trap for errors (at least trying to
learn how to trap for errors). I arranged the data to specifically error
when calling this executable.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "intupld.exe";
process.StartInfo.Arguments = hupFile;
process.StartInfo.WorkingDirectory = hencePath;
process.Start();
if (process.ExitCode != 0)
{
}
However: my .ExitCode is ALWAYS 0, regardless of if an error occured using
this exectable. When I run the executable from a dos prompt: I am able to
see the errors using the same file.
How would I actually be able to trap and find the errors that have occured
while running this executable?
Thanks
Andy
Jon Skeet [C# MVP]
2006-02-20 18:25:33 UTC
Permalink
Post by Andy
I am calling out to an executable using the System.Diagnostics.Process
methods and specifically attempting to trap for errors (at least trying to
learn how to trap for errors). I arranged the data to specifically error
when calling this executable.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "intupld.exe";
process.StartInfo.Arguments = hupFile;
process.StartInfo.WorkingDirectory = hencePath;
process.Start();
if (process.ExitCode != 0)
{
}
However: my .ExitCode is ALWAYS 0, regardless of if an error occured using
this exectable. When I run the executable from a dos prompt: I am able to
see the errors using the same file.
But what was the process exit code in dos? I suspect it was 0 there
too.
Post by Andy
How would I actually be able to trap and find the errors that have occured
while running this executable?
Well, you can redirect StdErr and StdOut from the process, read them in
your code, and then determine whether or not there was an error.
--
Jon Skeet - <***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Andy
2006-02-20 19:31:17 UTC
Permalink
Post by Jon Skeet [C# MVP]
Well, you can redirect StdErr and StdOut from the process, read them in
your code, and then determine whether or not there was an error.
When I run the following code though: my output and output2 variables are
both empty strings. Not sure why this should be the case. There should be
some value I would assume.

process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
output = process.StandardError.ReadToEnd();
output2 = process.StandardOutput.ReadToEnd();
process.WaitForExit();

Thanks
Andy
Jon Skeet [C# MVP]
2006-02-20 22:48:50 UTC
Permalink
Post by Andy
Post by Jon Skeet [C# MVP]
Well, you can redirect StdErr and StdOut from the process, read them in
your code, and then determine whether or not there was an error.
When I run the following code though: my output and output2 variables are
both empty strings. Not sure why this should be the case. There should be
some value I would assume.
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
output = process.StandardError.ReadToEnd();
output2 = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Well, it depends on how the program is writing out its data. Have you
tried the above with a small test program that definitely *does* write
to standard output and standard error? (A simple C# program would do
just fine.)

Note that in general doing it like that in one thread is a bad idea, as
if there's a lot of output, it may block on reading the *error* stream,
waiting for some of the output data to be read (which it won't until
the program has finished, which is when the error stream would be
closed). You'd generally read the two streams in different threads.
--
Jon Skeet - <***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Continue reading on narkive:
Loading...