4

我正在尝试使以下代码正常工作,以便可以从我的 c# 程序中调用 perl 脚本。我正在 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,消息框是空的。

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

谢谢

4

2 回答 2

6

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 之类的文件中看到用户文件。

于 2009-10-20T05:21:07.007 回答
0

perl 在你的%PATH%? 打开命令提示符并输入“ perl -v

于 2009-10-20T05:09:57.543 回答