当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Why can't I invoke a Perl script with ASP.NET's System.Diagnostics.Process?

问题描述:

I'm trying to use the System.Diagnostics.Process class to run a Perl script from within an ASP.NET application. The Perl command runs fine when I type it into the command line, but when ASP.NET tries to run the same command it doesn't work.

I suspect it may have to do with input and output stream. The Perl script takes a long time to run to completion on the command line. But in .NET the command returns almost immediately, and when I try to redirect Perl script's standard output to see what messages are yielded it shows just the first line and returns. Using blocking stream reading techniques doesn't fix this; .NET really thinks that the Perl script is outputting just one line, then finishes.

Any suggestions? As much as I would like to, the script can't be rewritten in .NET. Here's the code I'm using to launch the process. I've tried changing ReadToEnd() to a series of ReadLine() calls, but after the first call returns the first line of output the next call returns null.

Process proc = new Process();

proc.StartInfo.FileName = scriptPath;

proc.StartInfo.Arguments = arguments;

proc.StartInfo.RedirectStandardOutput = true;

proc.StartInfo.UseShellExecute = false;

proc.Start();

string output = proc.StandardOutput.ReadToEnd(); // Returns only the first line

proc.WaitForExit();

网友答案:

Switch the 2 last lines:

lineproc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd(); // Returns only the first 

or build a loop that keeps reading from proc.StandardOutput until the script has finished.

分享给朋友:
您可能感兴趣的文章:
随机阅读: