By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

Process.ExitCode() == 0

P: n/a
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
Feb 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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


"Andy" wrote:
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

Feb 20 '06 #2

P: n/a
Andy <An**@discussions.microsoft.com> wrote:
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.
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 - <sk***@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
Feb 20 '06 #3

P: n/a
Jon:

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
Feb 20 '06 #4

P: n/a
Andy <An**@discussions.microsoft.com> wrote:
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 - <sk***@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
Feb 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.