title: Wrapper for a Command Line Tool in C#
body: <p>Using <a href="http://support.microsoft.com/kb/305994" rel="noreferrer">MSDN</a> I got the class to write a wrapper for my command line tool. </p>
  
  <p>I now am facing a problem, if I execute the exe through the command line with arguments, it works perfect without any errors.</p>
  
  <p>But when I try to pass the arguments from the Wrapper it crashes the program.</p>
  
  <p>Wanted to know if I am passing the arguments properly and if I am wrong, could somebody point out please.
  This is the LaunchEXE class from MSDN</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Diagnostics;
  using System.IO;
  
  namespace SPDB
  {
      /// &lt;summary&gt;
      /// Class to run any external command line tool with arguments
      /// &lt;/summary&gt;
      public class LaunchEXE
      {
          internal static string Run(string exeName, string argsLine, int timeoutSeconds)
          {
              StreamReader outputStream = StreamReader.Null;
              string output = "";
              bool success = false;
  
              try
              {
                  Process newProcess = new Process();
                  newProcess.StartInfo.FileName = exeName;
                  newProcess.StartInfo.Arguments = argsLine;
                  newProcess.StartInfo.UseShellExecute = false;
                  newProcess.StartInfo.CreateNoWindow = true; //The command line is supressed to keep the process in the background
                  newProcess.StartInfo.RedirectStandardOutput = true;
                  newProcess.Start();
                  if (0 == timeoutSeconds)
                  {
                      outputStream = newProcess.StandardOutput;
                      output = outputStream.ReadToEnd();
                      newProcess.WaitForExit();
                  }
                  else
                  {
                      success = newProcess.WaitForExit(timeoutSeconds * 1000);
  
                      if (success)
                      {
                          outputStream = newProcess.StandardOutput;
                          output = outputStream.ReadToEnd();
                      }
                      else
                      {
                          output = "Timed out at " + timeoutSeconds + " seconds waiting for " + exeName + " to exit.";
                      }
                  }
              }
              catch (Exception e)
              {
                  throw (new Exception("An error occurred running " + exeName + ".", e));
              }
              finally
              {
                  outputStream.Close();
              }
  
              return "\t" + output;           
  
          }
      }
  }
  </code></pre>
  
  <p>This is the way I am passing arguments from my main program (Form1.cs)</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
          {
              string output;
              output = LaunchEXE.Run(@"C:\Program Files (x86)\MyFolder\MyConsole.exe", "/BACKUP C:\\MyBackupProfile.txt", 100);
              System.Windows.Forms.MessageBox.Show(output);
          }
  </code></pre>
  
  <p>The command line tool accepts the following command and works perfectly:</p>
  
  <p>C:\Program Files (x86)\MyFolder><strong>MyConsole.exe /BACKUP C:\MyBackupProfile.txt</strong></p>