I have followed the article about Write and read to cmd line from GUI on the post located here.

I have downloaded the files RedirectStandardOutput.zip posted by Diamonddrake. I found these to be exactly what i wanted to include in my own form and attempted to copy the code into my existing form. My problem is this:

Instead of this: this.console1 = new RedirectStandardOutput.Console();
I have: this.console1 = new System.Windows.Forms.TextBox();

My form is called MainForm1 and so I have renamed my namespace in Console1.cs from RedirectStandardOutput to MainForm1 to reflect this.

My form fails to find the reference.
In Form1.Designer.cs I have: private System.Windows.Forms.TextBox console1;

But in the downloaded files there is: private Console console1;
as well as other code relating to console.

I hope someone understands what I have done wrong and offer some tips.

If you look closely. Console1 is a custom control that derives from textbox. The redirect standard output needs a stream. The Console class redirects text from the stream to a textbox, and vise versa. The console control code from that project is as follows.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace RedirectStandardOutput
{
    public class Console : TextBox
    {
        public Console()
            : base()
        {
            this.Multiline = true;
            this.KeyPress += new KeyPressEventHandler(Console_KeyPress);

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        private void Console_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (InputStream != null)
            {
                if ((int)e.KeyChar == 13)
                {
                    InputStream.WriteLine();
                }
                else
                {
                    InputStream.Write(e.KeyChar);
                }
                InputStream.Flush();
            }
        }

        public void Start()
        {
            this.Text = string.Empty;
            if (OutputStream != null)
            {
                MethodInvoker readOutputStream = new MethodInvoker(ReadOutput);
                readOutputStream.BeginInvoke(null, null);
            }
        }

        public void ReadOutput()
        {
            int value;
            while ((value = OutputStream.Read()) != -1)
            {
                Invoke((MethodInvoker)delegate
                {
                    this.Text += (char)value;
                });
            }
        }

        public StreamWriter InputStream
        {
            get;
            set;
        }

        public StreamReader OutputStream
        {
            get;
            set;
        }
    }
}

That is your problem. you are trying to use use a standard textbox to eat the stream, And that is not quite how it works.

Thanks Diamonddrake.
I found my error. It now works as intended.

May I ask another question?

I have a button which loads cmd.exe or any other file of my choice.

CurrentProcess.StartInfo.FileName = "tracert.exe";

In this example it loads tracert.exe! what I am trying to do is load an IP 192.168.1.254 into the process on button_click from a textfield on my form!

private void tracertbtn2_Click(object sender, EventArgs e)
        {
            Process CurrentProcess = new Process();
            CurrentProcess.StartInfo.FileName = "tracert.exe";

            CurrentProcess.StartInfo.UseShellExecute = false;
            CurrentProcess.StartInfo.CreateNoWindow = true;
            CurrentProcess.StartInfo.RedirectStandardOutput = true;
            CurrentProcess.StartInfo.RedirectStandardInput = true;
            CurrentProcess.StartInfo.RedirectStandardError = true;

            CurrentProcess.Start();

            console2.InputStream = CurrentProcess.StandardInput;
            console2.OutputStream = CurrentProcess.StandardOutput;
            console2.Start();
        }

My question is therefore how do I insert the IP from my textbox into the console to get the desired results?

I am not familiar with that console app, but passing arguments is as simple as.

CurrentProcess.StartInfo.Arguments =  @"-ip 192.168.1.1";

Thanks Diamonddrake, in my console it was as simple as adding

CurrentProcess.StartInfo.Arguments = tracerttxtbx.Text;

after the code

CurrentProcess.StartInfo.FileName = "SYSTEM32" + "/" + "tracert.exe";

Brilliant cheers

Thanks Diamonddrake, in my console it was as simple as adding

CurrentProcess.StartInfo.Arguments = tracerttxtbx.Text;

after the code

CurrentProcess.StartInfo.FileName = "SYSTEM32" + "/" + "tracert.exe";

Brilliant cheers

Glad you got it working the way you wanted :)

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.