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

Recommended Answers

All 6 Replies

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

commented: you beat to posting while I was looking up my ref :) +6

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.

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);
        }
    }
}

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.

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.