• Welcome to Valhalla Legends Archive.
 

Starting a console program and reading it's output?

Started by Joe[x86], September 09, 2006, 10:16 PM

Previous topic - Next topic

Joe[x86]

I want to write a program that will start another console utility (namely ping.exe) and stream in and analyze it's output. Is there any easy way to do this?
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

MyndFyre

You can intercept console I/O by using the Process and ProcessStartInfo classes, and using the StreamReader from Process.StandardOutput:


using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace PingIntercept
{
    class Program
    {
        static void Main(string[] args)
        {
            Process proc = new Process();
            ProcessStartInfo psi = new ProcessStartInfo("ping.exe", "www.google.com");
            psi.CreateNoWindow = true;
            psi.RedirectStandardOutput = true;
            psi.UseShellExecute = false;

            proc.StartInfo = psi;
            Console.WriteLine("Pinging www.google.com......");

            proc.Start();

            string result = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit();

            Console.WriteLine(result);

            Console.WriteLine("Press <enter> to exit...");
            Console.ReadLine();
        }
    }
}


Output:


Pinging www.google.com......

Pinging www.l.google.com [66.102.7.99] with 32 bytes of data:

Reply from 66.102.7.99: bytes=32 time=43ms TTL=245
Reply from 66.102.7.99: bytes=32 time=42ms TTL=245
Reply from 66.102.7.99: bytes=32 time=43ms TTL=245
Reply from 66.102.7.99: bytes=32 time=42ms TTL=245

Ping statistics for 66.102.7.99:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 42ms, Maximum = 43ms, Average = 42ms

Press <enter> to exit...


You can also do line-by-line, for instance, by replacing ReadToEnd() with ReadLine() while the process is valid (try a while or do...while loop).
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Joe[x86]

#2
Go figure I'd fail at this. :p

snipped

I've added debugging code to display when tmrUpdate_Tick starts and stops, and both of them print just fine. I'm looking to write a program that has a label to state the highest ping, lowest ping, etc. Anyhow, do you see what's wrong?




I previously had cont = (s == null);[/tt which is the wrong value, and is now replaced with cont = !(s == null);. The output messages that are supposed to show when the function begins and ends don't show at all now with this code:

[edit]I replaced that pile of mess with some sensible code that does the exact same thing, with the exact same result, but looks better. :)[/edit]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace PingAnalyzer
{
    public partial class Main : Form
    {

        private Process proc;

        public Main()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            proc = new Process();
            ProcessStartInfo psi = new ProcessStartInfo("ping.exe", "-t www.google.com");
            psi.CreateNoWindow = true;
            psi.RedirectStandardOutput = true;
            psi.UseShellExecute = false;
            proc.StartInfo = psi;
            proc.Start();
            tmrUpdate.Enabled = true;
        }

        private void tmrUpdate_Tick(object sender, EventArgs e)
        {
            parse("Start" + Environment.NewLine);
            bool cont = true;
            while(cont)
            {
                String s = proc.StandardOutput.ReadLine();
                if (s == null)
                {
                    cont = false;
                }
                else
                {
                    parse(s);
                }
            }
            parse("Finish" + Environment.NewLine);
        }

        private void parse(String text)
        {
            richTextBox1.Text += text;
        }
    }
}
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Joe[x86]

Alright, I fixed it. It's mega-messy now so if anyone cares enough, IM me and I'll zip it or whatever.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.