[RESOLVED] Continously get output of a command line program
How can I run an external command line program and capture its output to a textbox? I've found how to run an external program until it ends and get all the output, but this is not what I want to do. This external program is a server and I would like to be able to parse each new thing it spits out. For example, the server might start up. Then a few minutes later it might spit out "Blahblah logged in." I would like to be able to parse this text with my C# program. Then I would like to be able to output to the command line program "tell blahblah Welcome back!"
So I suppose I have two questions. How do I monitor a program's output, and how do I input to that program?
Ninja Edit: Found this thread from only a few days ago. Only problem is, I can't translate the vb code :( I can't even get past
Code:
Private WithEvents MyProcess As Process
Re: Continously get output of a command line program
That's simply a field declaration. The WithEvents allows event handlers to be attached using a Handles clause on the method. There's no equivalent in C# so you just declare the field as normal and use += to attach an event handler as normal.
Re: Continously get output of a command line program
I'm very new to C#, so doing anything "as normal" requires study :o.
I can't even figure out which namespace "process" comes from so I can add it with the rest of the "using"s
Re: Continously get output of a command line program
The MSDN documentation will tell you the namespace for any and every type you care to lookup. That's just the sort of thing that documentation is for.
That said, the Process class is a component so, if you're using WinForms, the easiest option is to just add a Process instance to your form in the designer. You can then use the Properties window to create event handlers... as normal. ;)
Re: Continously get output of a command line program
So I've got the following code. Nothing is returned through the output textbox except "Process Started at 2/9/2011 21:52:28 PM"
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace CmdTest
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void DataReceivedEventHandler(object sender, DataReceivedEventArgs e)
{
OutputTextBox.AppendText("\r\n" + e.Data);
}
private void ErrorDataReceivedEventHandler(object sender, DataReceivedEventArgs e)
{
OutputTextBox.AppendText("\r\nError: " + e.Data);
}
private void frmMain_Load(object sender, EventArgs e)
{
MyProcess = new Process();
MyProcess.StartInfo.FileName = "CMD.EXE";
MyProcess.StartInfo.UseShellExecute = false;
MyProcess.StartInfo.CreateNoWindow = true;
MyProcess.StartInfo.RedirectStandardInput = true;
MyProcess.StartInfo.RedirectStandardOutput = true;
MyProcess.StartInfo.RedirectStandardError = true;
MyProcess.Start();
MyProcess.BeginErrorReadLine();
MyProcess.BeginOutputReadLine();
OutputTextBox.AppendText("Process Started at " + MyProcess.StartTime.ToString());
}
private void ExecuteButton_Click(object sender, EventArgs e)
{
MyProcess.StandardInput.WriteLine(InputTextBox.Text);
MyProcess.StandardInput.Flush();
InputTextBox.Text = "";
}
private void MyProcess_Exited(object sender, EventArgs e)
{
}
}
}
I can tell cmd.exe has been executed because I can see it in task manager. When I close my app, cmd.exe is killed also. Have any idea why my program won't show the output?
Re: Continously get output of a command line program
I don't see you handling any events.
Re: Continously get output of a command line program
Re: Continously get output of a command line program
Ah, so you added MyProcess in the designer. Then here is your problem:
Code:
MyProcess = new Process();
You've already got a Process object. You added it in the designer and handled its events. You then discard that process object and replace it with a new one, whose events you are not handling.
Re: Continously get output of a command line program
:D Thank you SO much! I completely overlooked that. (I got into copy/paste mode from this article.)
Edit: And unfortunately "You must spread some Reputation around before giving it to jmcilhinney again."
Hope my verbal thanks are enough :P