【问题标题】:perl.exe cannot be called by ProcessStartInfoperl.exe 不能被 ProcessStartInfo 调用
【发布时间】:2010-12-08 05:36:06
【问题描述】:

我正在尝试让以下代码工作,以便我可以从我的 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,消息框是空的。

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

谢谢

【问题讨论】:

    标签: c# perl processstartinfo


    【解决方案1】:

    perl.exe 可以处理命令行中包含空格的未加引号的路径吗?尝试引用路径:

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

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

    1. C:\Documents
    2. 设置\test_perl.pl

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

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

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

    【讨论】:

    • 我认为文件路径没有任何问题,因为如果我将 perl.exe 更改为 notepad.exe,它可以工作。我还故意删除了 myusername\Documents\。
    • ...还是试试吧。记事本!= perl。
    • 迈克尔,你很好!谢谢!它现在正在工作。但是为什么会有额外的“”呢?
    • 您应该将检查更改为此答案。
    • @alex:很高兴它有帮助。由于双引号用于分隔字符串,因此如果要将它们包含在 inside 字符串中,则需要对引号进行转义。在标准字符串中,您将为此使用反斜杠 (\")。在这里,您将字符串标记为带有“@”符号的逐字字符串。要在逐字字符串中转义双引号,请将它们加倍 (“” )。
    【解决方案2】:

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

    【讨论】:

    • 是的,我可以在dos窗口执行perl xxx.pl或xxx.txt。
    • 我很困惑,您将此标记为正确,但它似乎仍然不适合您。我刚试过这个,它对我有用。您确定 perl 程序会打印任何内容吗?你确定它在标准输出上打印吗?在控制台程序而不是 Windows 应用程序中尝试。 perl 程序的内容是什么?
    猜你喜欢
    • 1970-01-01
    • 2016-08-14
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多