I am trying to read a line from the command prompt and output that line to a text box in my C# application. The only catch is i can't seem to get it to read a line at a time and display it in the text box as the command prompt is running its command.

for example

string output = "";
while (output != null)
{
output = console.StandardOutput.ReadLine();
OutputBox.Text += '\n' + output;
}

Does anyone know how to read a command prompt line by line and output it into a text box as it is occurring?

Recommended Answers

All 2 Replies

Use the following code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "";
            Sample a = new Sample();
            a.Refresh();   // re-paint a form
            do
            {
                s = Console.ReadLine();
                a.MyText = s;
                a.Refresh();  // re-paint a form
            } while (s.Length != 0);
        }
        static void show()
        {

        }
    }
}

class Sample : System.Windows.Forms.Form
{
    System.Windows.Forms.TextBox t = new System.Windows.Forms.TextBox();
    public Sample()
    {
        t.Multiline = true;
        t.Size = new System.Drawing.Size(100, 100);
        Controls.Add(t);
        Show();
    }
    public string MyText
    {
        get
        {
            return t.Text;
        }
        set
        {
            t.Text = t.Text + "\r" + "\n" + value;
        }
    }

}

Thank you, works perfect.

Use the following code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "";
            Sample a = new Sample();
            a.Refresh();   // re-paint a form
            do
            {
                s = Console.ReadLine();
                a.MyText = s;
                a.Refresh();  // re-paint a form
            } while (s.Length != 0);
        }
        static void show()
        {

        }
    }
}

class Sample : System.Windows.Forms.Form
{
    System.Windows.Forms.TextBox t = new System.Windows.Forms.TextBox();
    public Sample()
    {
        t.Multiline = true;
        t.Size = new System.Drawing.Size(100, 100);
        Controls.Add(t);
        Show();
    }
    public string MyText
    {
        get
        {
            return t.Text;
        }
        set
        {
            t.Text = t.Text + "\r" + "\n" + value;
        }
    }

}
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.