Redirecting standard error out

264 views Asked by At

So what i am doing is calling java.exe and making a call to a jar file with some arguments. If everyting is fine and the command works, then the arguments are printed out to a richtext box.

The issue I am having is when the command isn't correct. So in once instance, say the user types in the wrong password that's passed into txtPassword. Stanadard out is not being redirected. If i run the exact same command in a Dos console, the message "Error: Auth fail", is being presented. How do i redirect that error to the rich text box? I thought redirecting stdout would do it, but apparently not.

Any help would be apprecaited. Please see the code below.

  //Declare and instantiate a new process component.
                System.Diagnostics.Process process1;
                process1 = new System.Diagnostics.Process();
                process1.StartInfo.UseShellExecute = false;
                process1.StartInfo.RedirectStandardOutput = true;
                process1.StartInfo.CreateNoWindow = true;

                process1.StartInfo.FileName = "java.exe ";

                toLoad = lstBarToLoad.Items[i].Text;

               process1.StartInfo.Arguments = "-Xmx512M -jar Deploy.jar" + txtPassword;
               process1.StartInfo.Arguments += toLoad;

              Console.WriteLine(process1.StartInfo.Arguments);

              process1.Start();
              process1.OutputDataReceived += (s, a) => myMethod(a);
              process1.BeginOutputReadLine();

   //myMthod
     private void myMethod(DataReceivedEventArgs e)
    {
         if (e.Data != null)
        {
            Action action = () => rchsdtOut.Text += "\r\n" + e.Data.ToString();
            rchsdtOut.BeginInvoke(action, null);
            Console.WriteLine(e.Data.ToString());
        }
    }//end of private
2

There are 2 answers

5
Jamie Dixon On BEST ANSWER

As well as using

process1.StartInfo.RedirectStandardOutput = true;

You also need to use

process1.StartInfo.RedirectStandardError = true;

Gets or sets a value that indicates whether the error output of an application is written to the Process.StandardError stream

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandarderror.aspx

You can then read out the error and do with it as you please:

string error = process1.StandardError.ReadToEnd();
0
TcKs On

You can use RedirectStandardError property same way as you use the RedirectStandardOutput property.