title: Redirecting cmd Output to textBox
body: <p>I'm Re-Creating the "command prompt" into a Windows Form.
  The application is not working properly; and i can't figure exactly why.</p>
  
  <p>When the form Loads it is suposed to run the cmd.exe (Load the cmd info into "TextBox_Receive"), but it doesnt; and also after writing any command in the "textBox_send" (that sends input); it will only show input after pressing "Enter" key 2 or 3 times.</p>
  
  <p>Any idea what i am missing here?</p>
  
  <pre><code>public partial class Form1 : Form
  {
      // Global Variables:
      private static StringBuilder cmdOutput = null;
      Process p;
      StreamWriter SW;
  
      public Form1()
      {
          InitializeComponent();
          textBox1.Text = Directory.GetCurrentDirectory();
          // TextBox1 Gets the Current Directory
      }
  
      private void Form1_Load(object sender, EventArgs e)
      {
          checkBox1.Checked = true;
          // This checkBox activates / deactivates writing commands into the "textBox_Send"
  
          cmdOutput = new StringBuilder("");
          p = new Process();
  
          p.StartInfo.FileName = "cmd.exe";
          p.StartInfo.UseShellExecute = false;
          p.StartInfo.CreateNoWindow = true;
          p.StartInfo.RedirectStandardInput = true;
          p.StartInfo.RedirectStandardOutput = true;
          p.StartInfo.RedirectStandardError = true;
          p.EnableRaisingEvents = true;
  
          p.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
  
          p.Start();
  
          SW = p.StandardInput;
  
          p.BeginOutputReadLine();
          p.BeginErrorReadLine();            
      }
  
      private static void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
  // I dont actually understand this part of the code; as this is a "copy" of a snippet i found somewhere. Although it fixed one of my issues to redirect.
      {
          if (!String.IsNullOrEmpty(outLine.Data))
          {
              cmdOutput.Append(Environment.NewLine + outLine.Data);
          }
      }
  
      private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
          // Send "Enter Key" - Send Command
          if (e.KeyChar == 13)
          {
              SW.WriteLine(txtbox_send.Text);
              txtbox_receive.Text = cmdOutput.ToString();
              txtbox_send.Clear();
          }
      }
  
      private void checkBox1_CheckedChanged(object sender, EventArgs e)
      {
          // Enable / Disable Sending Commands
          if (checkBox1.Checked)
              txtbox_send.Enabled = true;
          else
              txtbox_send.Enabled = false;
      }
  }
  </code></pre>
  
  <p>}</p>