Results 1 to 9 of 9

Thread: [RESOLVED] Continously get output of a command line program

  1. #1

    Thread Starter
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Resolved [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
    Last edited by half flung pie; Feb 9th, 2011 at 08:50 PM. Reason: derp.

    Base 2
    Fcnncu"Nqxgu"Lguug##

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Re: Continously get output of a command line program

    I'm very new to C#, so doing anything "as normal" requires study .
    I can't even figure out which namespace "process" comes from so I can add it with the rest of the "using"s

    Base 2
    Fcnncu"Nqxgu"Lguug##

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    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?

    Base 2
    Fcnncu"Nqxgu"Lguug##

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Continously get output of a command line program

    I don't see you handling any events.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Re: Continously get output of a command line program

    Do you mean doing this?

    Base 2
    Fcnncu"Nqxgu"Lguug##

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Re: Continously get output of a command line program

    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

    Base 2
    Fcnncu"Nqxgu"Lguug##

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width