Tuesday, 8 October 2013

Csharp - Redirect Output of Process

RedirectStandardOutput eliminates the need for output files. It allows us to use a console program directly inside a C# program.

Related Article Process in C#:
C# (CSharp) Process - .NET Framework

I wanted to launch a script from a Windows Form application and display the standard output in a text box as the process was running. Surely you're not surprised to learn that multithreading is involved. It turns out you'll have at least four threads running to do this simple task.

A script or executable can be run using System.Diagnostics.Process. The string FileName is set to the executable (e.g. perl.exe, Run.bat, ConsoleApplication.exe). The string Arguments is set to the command-line arguments for that executable (e.g. perlscript.pl, filename1.txt filename2.txt, etc). The following code will start that executable.

I will use Paping executable on this example.
Here are syntax option paping.exe command
Now download Paping here and paste in your project folder bin\Debug
Design your Windows Form project

We have 1 Label , 2 TextBox and 2 Button
Code inside Form is

        delegate void SetTextCallback(string text);

        public Form1()
        {
            InitializeComponent();
        }
 
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.textBox1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox2.AppendText(text + Environment.NewLine);
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Process process;
            process = new Process();
            process.StartInfo.FileName = Application.StartupPath + "\\paping.exe";
            process.StartInfo.Arguments = textBox1.Text;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);

            process.Start();
            process.BeginOutputReadLine();
        }

        public void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            SetText(outLine.Data);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Process[] pcs_check = Process.GetProcessesByName("paping");
            if (pcs_check.Length > 0)
                foreach (var p in pcs_check)
                    p.Kill();
        }

The ouput of paping command
www.google.com -p 80 -c 4
it's mean ping address google with port 80 and count until 4 time.


Download full source project "RedirectStandardOutput-Paping"