title: Embedding a CMD terminal in a C# Winforms application
body: <p>What I intend to do is build an application which, among other things, will have a command line embedded in it just like some IDEs do (something I find extremely useful). </p>
  
  <p>This is the code that I have so far, do note that it's a Winforms project: </p>
  
  <pre><code>public partial class Form1 : Form
      {
          Process p = new Process();
          ProcessStartInfo info = new ProcessStartInfo();
  
          public Form1() {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e) {
              info.FileName = "cmd.exe";
              info.RedirectStandardInput = true;
              info.RedirectStandardOutput = true;
              info.RedirectStandardError = true;
              info.UseShellExecute = false;
              info.CreateNoWindow = true;
  
              p.StartInfo = info;
              p.Start();
          }
  
          private void button1_Click(object sender, EventArgs e) {
              using(StreamWriter sw = p.StandardInput) {
                  if(sw.BaseStream.CanWrite) {
                      sw.WriteLine(textBox1.Text);
                  }
              }
              textBox2.Text = p.StandardOutput.ReadToEnd();
              textBox3.Text = p.StandardError.ReadToEnd();
              p.WaitForExit();
          }
      }
  }
  </code></pre>
  
  <p>As you can see there are 3 textboxes and one button: </p>
  
  <ul>
  <li>textbox1 is for entering the command</li>
  <li>textbox2 is for stdout</li>
  <li>textbox3 is for stderr</li>
  </ul>
  
  <p>On to my problem: </p>
  
  <p>I can only input <em>one</em> command because after executing it, my CMD window vanishes. I know it dies off because I've set <code>info.CreateNoWindow = false;</code> and it indeed vanishes and if I try to enter another command I get an exception. </p>
  
  <p><strong>How would I go on about keeping my CMD window 'alive' so that I can use it as much as I please? In short I want to truly mimic CMD behavior.</strong> </p>
  
  <p>Feel free to ask for more information if something is not clear. </p>
  
  <p><strong>Extra info/What I tried:</strong>  </p>
  
  <p>I've tried adding <code>info.Attributes = "/K";</code> since I know that <code>/K</code> should keep the CMD alive. I've also read that <code>p.WaitForExit();</code> should keep the CMD alive, but from what I figured this is only for the purpose of reading the output. Needless to say, I do not need that since I'm already redirecting its output. Neither of these solutions work but it is entirely possible that I'm using them the wrong way.  </p>
  
  <p>I need that process alive so I can easily navigate using <code>cd</code> and executing a sequence of commands when needed, such as when accessing <code>ftp</code> or <code>mysql</code>. I know I can work around these two examples with parameters, but not for every application. In short, spawning a new process every time is not something I want. I want that CMD interface to be up at all times. </p>
  
  <p>The cmd process dies after </p>
  
  <pre><code>using(StreamWriter sw = p.StandardInput) {
      if(sw.BaseStream.CanWrite) {
          sw.WriteLine(textBox1.Text);
      }
  }
  </code></pre>
  
  <p>But I cannot pinpoint why. </p>