Author | Message | Time |
---|---|---|
JoeTheOdd | 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? | September 10, 2006, 3:16 AM |
Myndfyr | You can intercept console I/O by using the Process and ProcessStartInfo classes, and using the StreamReader from Process.StandardOutput: [code] 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(); } } } [/code] Output: [code] 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... [/code] 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). | September 10, 2006, 10:23 AM |
JoeTheOdd | Go figure I'd fail at this. :p [code]snipped[/code] 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? [hr] I previously had [tt]cont = (s == null);[/tt which is the wrong value, and is now replaced with [tt]cont = !(s == null);[/tt]. 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] [code]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; } } }[/code] | September 10, 2006, 12:48 PM |
JoeTheOdd | Alright, I fixed it. It's mega-messy now so if anyone cares enough, IM me and I'll zip it or whatever. | September 10, 2006, 9:41 PM |