Hello All,
Im trying to write a little gui for a command line application i have to run everyday. The command line app is run as follows:
stcmpgo -s http://localhost -u username -p password -a -n -i filename.xml
There are multiple flags that can be set which I will use check boxes to add them to the command line as not all are used each time.
I have a button set up to run the command and would like to have the output goto a text box with in the application. The application doesn't seem to run. Currently im just getting started so im trying to display a directory listing in the textbox just so I can get an understand on how to get the output to display. Then I can build from there.

Any advice?

I tried using code as follows:

Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "dir";
            p.Start();
            string Output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            outputTextbox.Text = Output;

dir isn't actually an executable; it is a command that Command Prompt understands. You could replace line 4 with:

p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "/c dir";

That should work for you.

Hey thanks! that worked great. Now im able to execute my code but it pops open a cmd prompt then displays the result. Is there a way to more streamline the result directly into the textbox?

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            String line;
            Process p = new Process();

            p.StartInfo.Arguments = "/c dir";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "cmd";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;

            p.Start();
            p.WaitForExit();

            StreamReader sr = p.StandardOutput;
            line = sr.ReadToEnd();
            textBox1.Text = line;
        }
    }
}

momerath, thanks that looks like it disabled the cmd window from being opened.
I started running the command I'm using and its partially working.

It looks like it has the starting line and ending line but im trying to run the help menu and thats not displaying. Im poking around now to see if I can get the app to run with a few more commands and see what i may be missing. Appreciate all the help so far

Yes, it does disable the viewing of the command window. If you want to see the command window flash on the screen you can change (or just remove) line 20.

If you are trying to interact with a command line program (one that doesn't end after you start it but expects user input) you'll have to redirect the standard input and write to that stream. You'll also want to remove the WaitForExit() as it won't ever stop waiting :)

For example i run the command in a command prompt and get the following output

12:50:53.300 stcmpgo started
Error: Required parameter TTYV not set.
Please correct above error and rerun.

12:50:53.352 stcmpgo ended

But in my output in my application im only seeing

12:50:53.300 stcmpgo started
12:50:53.352 stcmpgo ended

That's because the other messages are being sent to StandardError, not StandardOutput. You can redirect that, too :)

Just a quick note. WaitForExit() should be called after reading the stream. Although it is not likely to occur, this method will enter a deadlock if the stream's buffer becomes full before the program is able to exit.

I've tried Momerath's solution, and it seems like the exe is launched, but it freezes the form until you kill the exe process, and no text shows in my listbox (which i sused instead of a textbox:

private void bttnCpuStartMining_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("--url=");
            sb.Append(txtBoxCpuPool.Text);
            sb.Append(" -u ");
            sb.Append(txtBoxCpuUsername.Text);
            sb.Append(" -p ");
            sb.Append(txtBoxCpuPassword.Text);            

            String line;

            Process p = new Process();

            p.StartInfo.Arguments = sb.ToString();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "minerd";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;

            p.Start();
            p.WaitForExit();

            StreamReader sr = p.StandardOutput;
            line = sr.ReadToEnd();
            lbCpuOutput.Text = line;

        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.