gpt4 book ai didi

c# - ProcessStartInfo 无法调用 perl.exe

转载 作者:太空狗 更新时间:2023-10-30 00:59:00 25 4
gpt4 key购买 nike

我正在尝试让下面的代码工作,这样我就可以从我的代码中调用一个 perl 脚本C# 程序。我正在 xp service pack3 上使用 visual stdio 2008 进行开发。

 myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
myProcessStartInfo.Arguments = @"C:\Documents and Settings\test_perl.pl";
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcessStartInfo.CreateNoWindow = true;
myProcess.StartInfo = myProcessStartInfo;

myProcess.Start();
string output = myProcess.StandardOutput.ReadToEnd();
MessageBox.Show(output);
myProcess.WaitForExit();

我验证了 test_perl.pl 是否存在,如果我将 perl.exe 更改为 notepad.exe,则上面的代码有效。但是如果我使用 perl.exe,消息框是空的。

无法弄清楚为什么这是错误的。如果您知道原因,请帮助我。

谢谢

最佳答案

perl.exe 能否处理命令行中包含空格的不带引号的路径?尝试引用路径:

myProcessStartInfo.Arguments = @"""C:\Documents and Settings\test_perl.pl""";

由于命令行参数由空格分隔,除非引用文件路径,否则应用程序(在本例中为 perl.exe)将看到三个参数:

  1. C:\文档
  2. 设置\test_perl.pl

Perl 可能会尝试打开文件“C:\Documents”。这当然不存在。解决方案是引用包含空格的文件路径(或所有文件路径,以保持一致)。

您提到 notepad.exe 可以很好地处理不带引号的文件路径。很可能,这只是记事本比一般的熊更聪明,并为您合并了它的论点。

当然,还要验证该路径中是否存在文件。这实际上是一条不寻常的道路;通常,您会在 C:\Documents and Settings\myusername\Documents\file.ext 等位置看到用户文件。

关于c# - ProcessStartInfo 无法调用 perl.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1592580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com