Click to See Complete Forum and Search --> : "Catching" console errors


dfb78
January 1st, 2003, 12:07 PM
I've got a button in a form that executes a registration process that is required to run at the command line. I am trying to "catch" whether it runs correctly or not. Unfortunately, if the command returns something other than a simple line return (obviously noting a successful registration), I won't know it because the console simply goes to a new "C:\" prompt after anything anyway. Here is the registration method so far:

==============================================
public void startReg(string regCommand)
{
Process p = new Process();
StreamWriter sw;
StreamReader sr;
StreamReader err;

ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;

p.Start();
sw = p.StandardInput;
sr = p.StandardOutput;
err = p.StandardError;

sw.AutoFlush = true;
if (regCommand != "")
{
try
{
sw.WriteLine("cd ../");
sw.WriteLine("cd ../");
sw.WriteLine("cd ../");
sw.WriteLine("cd ../");
sw.WriteLine("cd ../");
sw.WriteLine("cd ../");
sw.WriteLine(regDrive + ":");
sw.WriteLine("cd " + cd1);
sw.WriteLine("cd " + cd2);
sw.WriteLine(regCommand);
regTimer.Enabled = true;
}
catch
{
MessageBox.Show("Error running registration command", "Error");
}
}
else
{
MessageBox.Show("ERROR!","There was an error trying to run the registration command.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
sw.Close();
}
==============================================

As you can see, I've TRIED to catch the error a couple ways, but then common sense kicked in and I realized the console will return a new line whether an error is returned or not (for instance: " 'command' is not recognized as an internal or external command, operable program or batch file ").

Thanks for any help.

MartinL
January 2nd, 2003, 07:09 AM
Hm... You are not catching any error created in the console. You are cathing just errors created by using WriteLine() member.

To see if the commands you are sending to the console just read and evaluate the streams returned by the console (read sr and err StreamReaders). Console should send all normal text into the stream you can read by sr StreamReader and all error messages to the stream you can read by err StreamReader object...

Martin

dfb78
January 2nd, 2003, 09:19 AM
Thanks, figured the rest out from there!