views:

433

answers:

3

I am using a thrid party software tool (command line tool) to merge PDF files together. Using C# I am attempting to use System.Diagnostics.Process to run the executable but I am coming up with a few errors depending on the parameter setup.

If UseShellExecute = true and RedirectStandardOutput = true I get: The Process object must have the UseShellExecute property set to false in order to redirect IO streams.

If UseShellExecute = true and RedirectStandardOutput = false I get: The system cannot find the file specified

If useShellExecute = false and RedirectStandardOutput = true I get: The system cannot find the file specified

If UseShellExecute = false and RedirectStandardOutput = false I get: The system cannot find the file specified

The code that is running is the following:

                    Process p = new Process();

                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardOutput = false;
                    p.StartInfo.WorkingDirectory = "C:\\Program Files (x86)\\VeryPDF PDF Split-Merge v3.0";
                    p.StartInfo.FileName = "pdfpg.exe " + strFileNames.Trim() + " " + D2P_Folder_Converted + "\\" + strOutputFileName;
                    p.Start();
                    p.WaitForExit();
                    p.Close();
                    p.Dispose();

Can someone help me get around this issue please

+1  A: 

When UseShellExecute is false the WorkingDirectory property changes its meaning! it becomes the working directory for the new process NOT the path to the executable. You need to specify the full path to the executable in the FileName property instead.

James Holland
Well see I initially tried your suggestion but then I was recieving the "The system cannot find the file specified" error.This is what I had originally:p.StartInfo.FileName = "C:\\Program Files (x86)\\VeryPDF PDF Split-Merge v3.0\\pdfpg.exe " + strFileNames.Trim() + " " + D2P_Folder_Converted + "\\" + strOutputFileName;
mattgcon
See Kevin's comment - you need to put the arguments to the executable in the Arguments property. Reference for the UseShellExecute problem is here: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute.aspx
James Holland
Thank you james, I was struggling on this..... how stupid of them to change the meaning of WorkingDirectory when UseShellExecute is disabled!
Dal
A: 

I'm no expert using the process API but it looks like you are placing command line arguments into the FileName. Try using Arguments for the command line arguments. And put the full path to the exe in the FileName.

Also using an @ in front of the string gets rid of the need for the doubling of backslashes.

p.StartInfo.FileName = @"C:\Program Files (x86)\VeryPDF PDF Split-Merge\pdfpg.exe" 
Kevin Gale
+2  A: 

Arguments shouldn't be passed in the FileName property. You should use the Arguments property for this:

p.StartInfo.Arguments = string.Format(
    "{0} {1}", 
    strFileNames.Trim(), 
    Path.Combine(D2P_Folder_Converted, strOutputFileName)
);
p.StartInfo.WorkingDirectory = Path.Combine(GetProgramFilesX86(), "VeryPDF PDF Split-Merge v3.0");
p.StartInfo.FileName = "pdfpg.exe";

where the GetProgramFilesX86 function is could be defined like so:

static string GetProgramFilesX86()
{
    var processorArchitecture = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432");
    if(IntPtr.Size == sizeof(long) || !string.IsNullOrEmpty(processorArchitecture))
    {
        return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
    }
    return Environment.GetEnvironmentVariable("ProgramFiles");
}
Darin Dimitrov
This suggestion merged with Kevin Gales "@" suggestion worked great. Thank you guys for helping.
mattgcon