Monday, February 27, 2012

Starting a Process (application) sending arguments and reading the Output/Error

Any application like notepad or calculator or user applications (console or windows) can be invoked using “Process” class which falls in “System.Diagnostics” package or namespace.

Below code demonstrates, creating a console application “Divide2Numbers” which accepts 2 arguments and writes the division of the given numbers to console.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Divide2Numbers
    class Program
        static void Main(string[] args)
            //getting the input arguments and writing the result of the division
            double a = double.Parse(args[0]);
            double b = double.Parse(args[1]);
            Console.Write("Division of {0} and {1} is {2}", a, b, (a / b));

A windows application is created which has 2 textboxes to enter value for “a” and “b”.
User will enter the values and click on “Divide” button.

The .exe created from above console application is copied to the current windows application directory.

In the button “Divide” click event, application will create a process object which will invoke the above console application exe and sends the a, b arguments.

private void Divide_Click(object sender, EventArgs e)
                //new process
                System.Diagnostics.Process p1 = new System.Diagnostics.Process();

                //sending the arguments (a,b) to divide application
                p1.StartInfo.Arguments = txtA.Text + " " + txtB.Text;

                //CreateNoWindow=false, to not open the process ui.
                p1.StartInfo.CreateNoWindow = false;

                //The application file path
                p1.StartInfo.FileName = Application.StartupPath + "/Divide2Numbers.exe";

                //UseShellExecute must be set to false, to redirect IO streams
                p1.StartInfo.UseShellExecute = false;

                //to redirect the error that occurs in the process
                p1.StartInfo.RedirectStandardError = true;

                //to redirect the output from the process
                p1.StartInfo.RedirectStandardOutput = true;

                //to start the process

                //reading the error and output from stream
                StreamReader oR = p1.StandardOutput;
                StreamReader eR = p1.StandardError;
                string strError = eR.ReadToEnd();
                string strOutput = oR.ReadToEnd();

                //displaying the error or result
                lblResult.Text = (strOutput.Trim().Length > 0) ? "Result : " + strOutput : "Error : " + strError;
            catch (Exception exp)
                MessageBox.Show("Error : " + exp.Message);

When user enters the value of a, b and click on “Divide” the application gets the result from console application through output stream.

Incase of Error, applications gets the error via Error Stream reader.

No comments:

Post a Comment