title: How to pass parameter to cmd.exe and get the result back into C# Windows application
body: <p>GUI of my program.
  <img src="https://i.stack.imgur.com/afUiu.jpg" alt="enter image description here"></p>
  
  <p>I want to pass parameter as <strong>dir</strong> to <strong>cmd.exe</strong> with <code>.NET</code> <code>Process</code> class and get correct output into the c# program.</p>
  
  <p>Source:</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.Threading;
  using System.Windows.Forms;
  
  namespace MyCmd
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
              comboBox1.Items.Add("cmd.exe");
              comboBox1.SelectedIndex = 0;
          }
  
          private Thread t;
  
          private void button1_Click(object sender, EventArgs e)
          {
              object[] param = new object[] { comboBox1.Text, txtParams.Text };
              if (param.Length &gt; 0 &amp;&amp; comboBox1.Text != "")
              {
                  t = new Thread(() =&gt; doTask(param));
                  t.IsBackground = true;
                  t.Start();
              }
              else
              {
                  MessageBox.Show("Invalid parameters!");
              }
          }
  
          private void doTask(object[] param)
          {
              Process proc = new Process
              {
                  StartInfo = new ProcessStartInfo
                  {
                      FileName = param[0].ToString(),
                      Arguments = param[1].ToString(),
                      UseShellExecute = false,
                      RedirectStandardOutput = true,
                      CreateNoWindow = true
                  }
              };
              proc.Start();
              while (!proc.StandardOutput.EndOfStream)
              {
                  string line = proc.StandardOutput.ReadLine();
                  this.Invoke((MethodInvoker)delegate
                  {
                      txtResponse.Text += line + Environment.NewLine;
                  });
              }
              proc.WaitForExit();
          }
      }
  }
  </code></pre>
  
  <p><strong>This is output</strong>
  <img src="https://i.stack.imgur.com/x88eo.jpg" alt="enter image description here"></p>
  
  <p>I want to launch <strong>cmd.exe</strong> and then enter <strong>dir</strong> then, it <strong>list the directory info into my application</strong>.</p>
  
  <p><strong>UPDATED:</strong></p>
  
  <p>Source updated:</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.Threading;
  using System.Windows.Forms;
  
  namespace MyCmdV2
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              comboBox1.Items.Add("cmd.exe");
              comboBox1.SelectedIndex = 0;
              proc = new Process();
  
              proc.StartInfo = new ProcessStartInfo
              {
                  FileName = "cmd.exe",
                  UseShellExecute = false,
                  RedirectStandardOutput = true,
                  RedirectStandardInput = true,
                  CreateNoWindow = true
              };
              proc.Start();
          }
  
          private Process proc;
  
          private Thread t;
  
          private void button1_Click(object sender, EventArgs e)
          {
              t = new Thread(() =&gt; doIt());
              t.IsBackground = true;
              t.Start();
          }
  
          private void doIt()
          {
              if (txtParams.Text.ToLower() == "cls")
              {
                  txtResponse.Text = "";
              }
              else
              {
                  proc.StandardInput.WriteLine(@txtParams.Text);
                  while (!proc.StandardOutput.EndOfStream)
                  {
                      string line = proc.StandardOutput.ReadLine();
                      this.Invoke((MethodInvoker)delegate
                      {
                          txtResponse.Text += line + Environment.NewLine;
                      });
                  }
                  proc.WaitForExit();
              }
          }
      }
  }
  </code></pre>
  
  <p>This is an example query only.I need to <strong>run any exe program</strong> with <strong>parameters</strong>.
  List all my program in to <code>ComboBox</code> and then pass the parameters in <code>TextBox</code>.</p>
  
  <p>Some time i need to pass parameter as <strong>IP</strong> address like this. <code>\\192.168.1.2</code> </p>
  
  <pre><code>Ex: psinfo.exe \\192.168.1.2 
  </code></pre>
  
  <p>But C# it's store as <code>\\\\192.168.1.2</code>. How to remove that escape sequence characters in parameter.</p>