title: Process.Start cmd.exe won't run cmd file that is passed as agument when running in IIS
body: <p>I've been searching and experimenting all morning with this one and I'm stumped.  I have an aspx page running in IIS and calling the following c# function.  I'm trying to have it run a cmd file and return the output from the cmd file.  I've experimented with the five different options in the code below:</p>
  
  <pre><code>protected String RunMyCmdFileAndGetResponse() {
      Process proc = new Process ();
      proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
  //  proc.StartInfo.Arguments = @"/c echo hello";                    &lt;== 1
  //  proc.StartInfo.Arguments = @"/c c:\mypath\myfile_badname.cmd";  &lt;== 2
  //  proc.StartInfo.Arguments = @"/c type c:\mypath\myfile.cmd";     &lt;== 3
      proc.StartInfo.Arguments = @"/c c:\mypath\myfile.cmd";      //  &lt;== 4
  //  proc.StartInfo.Arguments = @"/c call c:\mypath\myfile.cmd";     &lt;== 5
      proc.StartInfo.UseShellExecute = false;
      proc.StartInfo.RedirectStandardOutput = true;
      proc.StartInfo.RedirectStandardError = true;
      proc.StartInfo.CreateNoWindow = true;
  
      proc.Start();
      string response = proc.StandardOutput.ReadToEnd();
      response += proc.StandardError.ReadToEnd();
      proc.WaitForExit();
      return response;
  }
  </code></pre>
  
  <p>Cmd file c:\mypath\myfile.cmd contents are:</p>
  
  <pre><code>@echo test line 1 &gt; c:\mypath\myfilelog.txt
  @echo test line 2
  </code></pre>
  
  <p>This cmd file works as expected when run manually, producing myfilelog.txt and returning test line 2.  When executed with the c# code:</p>
  
  <ul>
  <li>Option 1 works - returns 'hello' response as expected.</li>
  <li>Option 2 fails as expected, indicating myfile_badname.cmd is not recognized as a valid command</li>
  <li>Option 3 works as expected - it returns the contents of myfile.cmd as the response - this confirms I am able to find and read the file.</li>
  <li>Option 4 does not work - as near as I can figure, it should.  It does not hang up, but also does not return any response at all, and does not execute the cmd file (no myfilelog.txt produced).</li>
  <li>Option 5 - same results as option 4.</li>
  </ul>
  
  <p>Note - I've also tried modifying myfile.cmd to remove line 1 (creating the log file) and only leave line 2 to echo a response.  Just in case it's a permission issue creating the log file.  Same result.</p>
  
  <p>Any help would be appreciated!</p>
  
  <p><strong>Updated to add solution:</strong></p>
  
  <p>The answer from @MaxOvrdrv got me thinking a different way.  There does indeed appear to be some kind of limitation when running Process.Start within an IIS context with UseShellExecute = false - if the primary argument is an executable file (cmd file, script file, etc), it will not run it.  I tried passing SomeExample.cmd to cmd.exe, and SomeExample.js to cscript.exe. </p>
  
  <p>However... I was able to trick it with a level of indirection, such that the executable file name is no longer the first argument, and it works just fine that way.  </p>
  
  <p>To run a cmd file:</p>
  
  <pre><code>string theResponse = RunMyCmdAndGetResponse(@"c:\somepath\mycmd.cmd");
  
  protected String RunMyCmdAndGetResponse(string cmdPath) {
      Process proc = new Process ();
      proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
      proc.StartInfo.Arguments = "/c cmd /c " + cmdPath;
      proc.StartInfo.UseShellExecute = false;
      proc.StartInfo.RedirectStandardOutput = true;
      proc.StartInfo.RedirectStandardError = true;
      proc.StartInfo.CreateNoWindow = true;
  
      proc.Start();
      proc.WaitForExit();
      string response = proc.StandardOutput.ReadToEnd();
      response += proc.StandardError.ReadToEnd();
      return response;
  }
  </code></pre>
  
  <p>To run a script file:</p>
  
  <pre><code>string theResponse = RunMyScriptAndGetResponse(@"c:\somepath\myscript.js");
  
  protected String RunMyScriptAndGetResponse(string scriptPath) {
      Process proc = new Process ();
      proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
      proc.StartInfo.Arguments = "/c cscript //nologo " + scriptPath;
      proc.StartInfo.UseShellExecute = false;
      proc.StartInfo.RedirectStandardOutput = true;
      proc.StartInfo.RedirectStandardError = true;
      proc.StartInfo.CreateNoWindow = true;
  
      proc.Start();
      proc.WaitForExit();
      string response = proc.StandardOutput.ReadToEnd();
      response += proc.StandardError.ReadToEnd();
      return response;
  }
  </code></pre>