CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2002
    Location
    Atlanta, GA
    Posts
    97

    "Catching" console errors

    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.

  2. #2
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    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

  3. #3
    Join Date
    Oct 2002
    Location
    Atlanta, GA
    Posts
    97
    Thanks, figured the rest out from there!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured