title: How do I execute ghostscript from C# program
body: <p>I am trying to call ghost script from my C# program, passing it some args to crop the footer of a PDF file, then overwrite the temp file with the new modified version. </p>
  
  <p>I think I'm calling the gs.exe incorrectly. Does anyone see a reason that the string I'm passing to start(gs) doesn't work? </p>
  
  <p>When trace the script, it triggers the catch when it gets to <code>System.Diagnostics.Process.Start(gs);</code> </p>
  
  <p><strong>This is the string that's being called in the process.start(gs) function</strong>  </p>
  
  <pre><code>C:\gs\gs9.14\bin\gswin64c.exe -o C:\Users\myname\Desktop\assignment1\assignment1\data\temp\test.pdf -sDEVICE=pdfwrite -c "[/CropBox [24 72 559 794] /PAGES pdf mark" -f C:\Users\myname\Desktop\assignment1\assignment1\data\temp\test.pdf
  </code></pre>
  
  <p><strong>This is the message that I get in my console.</strong> </p>
  
  <pre><code>System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
         at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
         at System.Diagnostics.Process.Start()
         at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
         at System.Diagnostics.Process.Start(String fileName)
         at assignment1.Program.cropPDFFooter(String tempPDF) in C:\Users\tessierd\Desktop\assignment1\assignment1\Program.cs:line 78
  </code></pre>
  
  <p><strong>Then this is the code for my method.</strong></p>
  
  <pre><code>public static void cropPDFFooter(string tempPDF)
      {
          try
          {
              byte[] croppedPDF = File.ReadAllBytes(tempPDF);
              string gsPath = @"C:\gs\gs9.14\bin\gswin64c.exe";
  
              List&lt;string&gt; gsArgsList = new List&lt;string&gt;();
              gsArgsList.Add(" -o " + tempPDF);
              gsArgsList.Add(" -sDEVICE=pdfwrite");
              gsArgsList.Add(" -c \"[/CropBox [24 72 559 794] /PAGES pdfmark\"");
              gsArgsList.Add(" -f " + tempPDF);
              var gsArgs = String.Join(null, gsArgsList);
  
              string gs = gsPath + gsArgs; // not needed anymore (see solution)
              // * wrong code here. 
              // System.Diagnostics.Process.Start(gs);
              // * Correct code below.
              System.Diagnostics.Process.Start(gsPath, gsArgs);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.ToString());
              Console.ReadLine();
          }
      }
  </code></pre>