工作目录的 System.Diagnostics.Process 问题

发布于 2024-08-21 02:56:28 字数 1167 浏览 16 评论 0原文

我正在使用第三方软件工具(命令行工具)将 PDF 文件合并在一起。使用 C#,我尝试使用 System.Diagnostics.Process 来运行可执行文件,但根据参数设置,我遇到了一些错误。

  • 如果 UseShellExecute = trueRedirectStandardOutput = true 我得到:
    • Process 对象必须将 UseShellExecute 属性设置为 false 才能重定向 IO 流。
  • 如果 UseShellExecute = trueRedirectStandardOutput = false 我得到:
    • 系统找不到指定的文件
  • 如果 useShellExecute = falseRedirectStandardOutput = true 我得到:
    • 系统找不到指定的文件
  • 如果 UseShellExecute = falseRedirectStandardOutput = 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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一江春梦 2024-08-28 02:56:28

UseShellExecutefalse 时,WorkingDirectory 属性会更改其含义!

它成为新进程的工作目录而不是可执行文件的路径。您需要在 FileName 属性中指定可执行文件的完整路径。

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.

薄暮涼年 2024-08-28 02:56:28

不应在 FileName 财产。您应该为此使用 Arguments 属性:

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";

其中 GetProgramFilesX86 函数可以这样定义:

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");
}

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");
}
反目相谮 2024-08-28 02:56:28

我不是使用流程 API 的专家,但看起来您正在将命令行参数放入 FileName 中。尝试使用参数作为命令行参数。并将exe的完整路径放入FileName中。

另外,在字符串前面使用 @ 可以消除双反斜杠的需要。

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

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" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文