954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# Command Prompt Reading

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?

DevC++4.9.9.2
Light Poster
39 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

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

}
__avd
Posting Genius (adatapost)
Moderator
8,647 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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

}
DevC++4.9.9.2
Light Poster
39 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You