ProcessStartInfo 无法调用 perl.exe
我正在尝试让以下代码正常工作,以便我可以从我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
perl.exe 可以处理命令行上包含空格的未加引号的路径吗?尝试引用路径:
由于命令行参数由空格分隔,除非引用文件路径,否则应用程序(在本例中为 perl.exe)将看到三个参数:
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:
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:
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.
perl 是否在您的
%PATH%
中?打开命令提示符并输入“perl -v
”Is perl in your
%PATH%
? Open a command prompt and type in "perl -v
"