工作目录的 System.Diagnostics.Process 问题
我正在使用第三方软件工具(命令行工具)将 PDF 文件合并在一起。使用 C#,我尝试使用 System.Diagnostics.Process 来运行可执行文件,但根据参数设置,我遇到了一些错误。
- 如果
UseShellExecute = true
和RedirectStandardOutput = true
我得到:- Process 对象必须将
UseShellExecute
属性设置为false
才能重定向 IO 流。
- Process 对象必须将
- 如果
UseShellExecute = true
和RedirectStandardOutput = false
我得到:- 系统找不到指定的文件
- 如果
useShellExecute = false
和RedirectStandardOutput = true
我得到:- 系统找不到指定的文件
- 如果
UseShellExecute = false
和RedirectStandardOutput = false
我得到:- 系统找不到指定的文件
正在运行的代码如下:
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();
有人可以帮我解决这个问题吗?
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.
- If
UseShellExecute = true
andRedirectStandardOutput = true
I get:- The Process object must have the
UseShellExecute
property set tofalse
in order to redirect IO streams.
- The Process object must have the
- If
UseShellExecute = true
andRedirectStandardOutput = false
I get:- The system cannot find the file specified
- If
useShellExecute = false
andRedirectStandardOutput = true
I get:- The system cannot find the file specified
- If
UseShellExecute = false
andRedirectStandardOutput = 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当
UseShellExecute
为false
时,WorkingDirectory
属性会更改其含义!它成为新进程的工作目录而不是可执行文件的路径。您需要在
FileName
属性中指定可执行文件的完整路径。When
UseShellExecute
isfalse
theWorkingDirectory
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.不应在 FileName 财产。您应该为此使用 Arguments 属性:
其中
GetProgramFilesX86
函数可以这样定义:Arguments shouldn't be passed in the FileName property. You should use the Arguments property for this:
where the
GetProgramFilesX86
function is could be defined like so:我不是使用流程 API 的专家,但看起来您正在将命令行参数放入 FileName 中。尝试使用参数作为命令行参数。并将exe的完整路径放入FileName中。
另外,在字符串前面使用 @ 可以消除双反斜杠的需要。
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.