title: Dos command in c# - RedirectStandardError/Output problem
body: <p>I need to run Dos commands within a class.  My problem is that the redirect options seem to prevent the command from running.<br>
  Here is my code:</p>
  
  <pre><code>public static int executeCommand(string cmd)
      {
          System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo("CMD.exe", "/C " + cmd);
          int exitCode = 0;
          //processStartInfo.RedirectStandardError = true;
          //processStartInfo.RedirectStandardOutput = true;
          processStartInfo.CreateNoWindow = true;
          processStartInfo.UseShellExecute = false;
          System.Diagnostics.Process process =
          System.Diagnostics.Process.Start(processStartInfo);
  
          process.WaitForExit(); //wait for 20 sec
          exitCode = process.ExitCode;
          //string stdout = process.StandardOutput.ReadToEnd();
          //string stderr = process.StandardError.ReadToEnd();
          process.Close();
          return exitCode;
      }
  </code></pre>
  
  <p>When I call xcopy:</p>
  
  <pre><code>if (executeCommand("xcopy.exe " + "/E /I /R /Y /Q c:\\temp\\*.* e:\\temp\\b1\\ ") != 0)
                  Log.Error("Error detected running xcopy ");
  </code></pre>
  
  <p>The method correctly runs xcopy.  If I want to redirect the SDTOUT and STDERR, the method returns 0 as well but xcopy didn't really run.</p>
  
  <p>In other words, this doesn't work:</p>
  
  <pre><code>public static int executeCommand(string cmd)
      {
          System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo("CMD.exe", "/C " + cmd);
          int exitCode = 0;
          processStartInfo.RedirectStandardError = true;
          processStartInfo.RedirectStandardOutput = true;
          processStartInfo.CreateNoWindow = true;
          processStartInfo.UseShellExecute = false;
          System.Diagnostics.Process process =
          System.Diagnostics.Process.Start(processStartInfo);
  
          process.WaitForExit(); //wait for 20 sec
          exitCode = process.ExitCode;
          string stdout = process.StandardOutput.ReadToEnd();
          string stderr = process.StandardError.ReadToEnd();
          process.Close();
          return exitCode;
      }
  </code></pre>
  
  <p>Any idea why?</p>
  
  <p>Thanks</p>
  
  <p>tony</p>