ProcessStartInfo 无法调用 perl.exe

发布于 2024-08-08 20:55:13 字数 877 浏览 2 评论 0原文

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

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

谢谢

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;
        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();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哑剧 2024-08-15 20:55:13

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

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

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

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

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

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

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

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.

她如夕阳 2024-08-15 20:55:13

perl 是否在您的 %PATH% 中?打开命令提示符并输入“perl -v

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文