开发者

perl.exe cannot be called by ProcessStartInfo

I am trying to get the following code to work so I can call a perl script from my c# program. I am developing using visual stdio 2008 on xp service pack3.

 myProcess = new Process();
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
        myProcessStartInfo.Arguments = @"C:\Documents and Settings\test_perl.pl";
        myProcessStartInfo.UseShellExecute = false;
        myProces开发者_高级运维sStartInfo.RedirectStandardOutput = true;
        myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        myProcessStartInfo.CreateNoWindow = true;
        myProcess.StartInfo = myProcessStartInfo;

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

I verify the test_perl.pl exists and If I change the perl.exe to notepad.exe, thie above code works. But if I use perl.exe, the message box is empty.

Can't figure out why this is wrong. Please help me if you know why.

Thanks


Can perl.exe handle unquoted paths containing spaces on the command line? Try quoting the path:

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

Since command-line arguments are delimited by spaces, unless the file path is quoted, the application (perl.exe, in this case) will see three arguments:

  1. C:\Documents
  2. and
  3. Settings\test_perl.pl

Perl will likely try to open the file "C:\Documents". This doesn't exist, of course. The solution is to quote file paths that contain spaces (or all file paths, to be consistent).

You mention that notepad.exe handles unquoted file paths fine. Likely, that's just notepad being smarter than the average bear, and merging its arguments for you.

And verify that a file exists at that path, of course. That's actually a slightly unusual path; normally, you'll see user files in something like C:\Documents and Settings\myusername\Documents\file.ext, or such.


Is perl in your %PATH%? Open a command prompt and type in "perl -v"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜