1,105,399 Community Members

cmd output to listbox

Member Avatar
Hendo
Light Poster
39 posts since Sep 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello all!

So, I'm giving a demo of a program that I've written for my boss. The program is loaded with test code that I use to ensure various options an routines are set properly, and show up in a listbox in the application (was going to remove it once the program was completed). One of the aspects of the program is that a command window pops up showing the results of an external application process. The boss would really like for me to remove the CMD window, and have the output of that piped into my listbox.

Is that even possible?

Thanks,
Hendo

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 403 [?]
Skill Endorsements: 35 [?]
 
1
 

to remove the CMD window

If you're using the Process class to start the external app, set StartInfo.CreateNoWindow to true.

have the output of that piped into my listbox

In that class you can also redirect the standard output of the process and put it into a variable. Here's an article on how

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

There is a more complete example here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline%28v=vs.90%29.aspx

Other than that follow tinstaffl advice.

Member Avatar
Hendo
Light Poster
39 posts since Sep 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm not getting it I guess. I have both articles up (from Tinstaafl and TnTinMN) and I've implemented those processes. I have mixed results from it though. No text is displayed...so I don't know what's going on. At first, I was trying to send the output of that command window to a listbox that I was using for troubleshooting purposes. Needless to say, the listbox did not display the output from the RedirectStandardOutput command.

So to test out the functions, I created a new form and did basic DIR commands using Process and RedirectStandardOutput. I had it going to a Listbox and a Textbox. It worked fine for the Textbox, but not the Listbox. Ok...so I go back to my program, and added a textbox to get the desired output, and I still got no result. Here's the code:

Process p = new Process();  
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;   
p.StartInfo.FileName = ScanS;
p.StartInfo.Arguments = allScanOpt;    
p.Start();      
string output = p.StandardOutput.ReadToEnd();
tbxOutput.Text = (output);   
p.WaitForExit();

So, to be clear on what's going on, the ScanS variable is the path and excecutable to Microsofts ScanState program. The allScanOpt variable is the series of switches need for the ScanState program to function as desired. All of that is good, but I'm not seeing the ScanState process logged in the textbox. If I couldn't see my HDD light going to town, then I'd swear my program has locked up and nothing was working.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

Attached is a simple command window redirector that you can follow as an example.

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;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        StreamWriter wtr;
        Process p;

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string cmd = textBox2.Text.Trim();
            if (!string.IsNullOrEmpty(cmd))
            {
                wtr.WriteLine(cmd);
            }

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            p.Kill();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ProcessStartInfo si = new ProcessStartInfo();
            {
                si.Arguments = "/k dir *.*";
                si.FileName = "cmd.exe";
                si.UseShellExecute = false;
                si.RedirectStandardOutput = true;
                si.RedirectStandardInput = true;
                si.CreateNoWindow = true;
            }

            p = new Process();
            p.StartInfo = si;
            p.OutputDataReceived += OutputHandler;
            p.Start();
            p.BeginOutputReadLine();
            wtr = p.StandardInput;
        }

        private void OutputHandler(object sendingProcess, DataReceivedEventArgs e)
        {
            if (textBox1.InvokeRequired)
            {
                this.Invoke(new System.Diagnostics.DataReceivedEventHandler(OutputHandler), new object[] {
                sendingProcess,
                e
            });
                return;
            }
            if (!string.IsNullOrEmpty(e.Data))
                textBox1.AppendText(e.Data + System.Environment.NewLine);
        }
    }
}
Member Avatar
Hendo
Light Poster
39 posts since Sep 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

That definitely gets me closer. The typical output of the scanstate.exe application is not showing up in the textbox, however once Scanstate is completed, it does return a status code in the textbox, which will be good enough for now.

I'm now behind schedule on this build, so I'm going to have to move forward on the other functions. I'll come back to this problem later on in the week. Thanks TnTinMN and tinstaafl.

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 403 [?]
Skill Endorsements: 35 [?]
 
0
 

Something to think about when you come back to this. Perhaps scanstate need the verbose option enabled('/v') to get any meaningful output.

/v Enables verbose output. The syntax is /v # where # is 1 to 7, with 1 being the least verbose, and 7 the most.

Or perhaps it's using the Standard Error Output not the Standard Output.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: