I am using a third 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.
UseShellExecute = true and RedirectStandardOutput = true I get:
UseShellExecute property set to false in order to redirect IO streams.UseShellExecute = true and RedirectStandardOutput = false I get:
useShellExecute = false and RedirectStandardOutput = true I get:
UseShellExecute = false and RedirectStandardOutput = false I get:
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?
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.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With