ProcessStartInfo无法调用perl.exe

ale*_*lex 4 c# perl processstartinfo

我试图让以下代码工作,所以我可以从我的c#程序调用perl脚本.我正在使用xd 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();
Run Code Online (Sandbox Code Playgroud)

我验证test_perl.pl存在,如果我将perl.exe更改为notepad.exe,则上述代码可以正常工作.但是,如果我使用perl.exe,则消息框为空.

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

谢谢

Mic*_*tta 6

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

myProcessStartInfo.Arguments = @"""C:\Documents and Settings\test_perl.pl""";
Run Code Online (Sandbox Code Playgroud)

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

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

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

你提到notepad.exe可以处理不带引号的文件路径.可能,这只是记事本比普通熊更聪明,并为你合并其论点.

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