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.
==============================================
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.