如何运行一个控制台应用程序,捕获输出,并在文字显示呢? [英] How do I run a Console Application, capture the output and display it in a Literal?
本文介绍了如何运行一个控制台应用程序,捕获输出,并在文字显示呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到,我可以的System.Diagnostics.Process启动进程。我用下面的代码很努力,但它不工作。该页面只是挂起,我必须重新启动IIS ...
使用系统;
使用System.Collections.Generic;
使用的System.Web;
使用System.Web.UI程序;使用System.Web.UI.WebControls
;使用System.Diagnostics程序
;
公共部分类VideoTest:System.Web.UI.Page
{
名单,LT;字符串> outputLines =新的List<串GT;();
布尔退出= FALSE;
保护无效的Page_Load(对象发件人,EventArgs五)
{
串APPPATH = Request.PhysicalApplicationPath;
流程myProcess =新工艺();
myProcess.StartInfo.UseShellExecute = FALSE;
myProcess.StartInfo.FileName = APPPATH +\\bin\\ffmpeg.exe
myProcess.StartInfo.CreateNoWindow = TRUE;
myProcess.OutputDataReceived + =新DataReceivedEventHandler(OutputHandler);
myProcess.Exited + =新的EventHandler(ExitHandler);
myProcess.Start();
,而(!退出)
{
//这是不好不好不好不好....
}
litTest.Text =;
的foreach(在outputLines串线)
litTest.Text + =行;
}
私人无效OutputHandler(对象sendingProcess,DataReceivedEventArgs轮廓)
{
outputLines.Add(outLine.Data);
}
//处理Exited事件和显示处理信息。
私人无效ExitHandler(对象发件人,发送System.EventArgs)
{
退出= TRUE;
}
}
解决方案
我已经做了非常相似的解决方案的东西 - 这是对我工作的罚款:
的ProcessStartInfo PINFO =新的ProcessStartInfo(CMD 。可执行程序);
pInfo.FileName = exePath;
pInfo.WorkingDirectory =新的FileInfo(exePath).DirectoryName;
pInfo.Arguments = ARGS;
pInfo.CreateNoWindow = FALSE;
pInfo.UseShellExecute = FALSE;
pInfo.WindowStyle = ProcessWindowStyle.Normal;
pInfo.RedirectStandardOutput = TRUE;
进程p =的Process.Start(PINFO);
p.OutputDataReceived + = p_OutputDataReceived;
p.BeginOutputReadLine();
p.WaitForExit();根据返回代码
//设置状态。
如果(p.ExitCode == 0)this.Status = StatusEnum.CompletedSuccess;
,否则this.Status = StatusEnum.CompletedFailure;
有趣的差异似乎是使用WaitForExit()的,也可能是BeginOutputReadLine()。
I see that I can start processes with System.Diagnostics.Process. I'm trying with the following code, but its not working. The page just hangs and I have to restart IIS...
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
public partial class VideoTest : System.Web.UI.Page
{
List<string> outputLines = new List<string>();
bool exited = false;
protected void Page_Load(object sender, EventArgs e)
{
string AppPath = Request.PhysicalApplicationPath;
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = AppPath + "\\bin\\ffmpeg.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
myProcess.Exited += new EventHandler(ExitHandler);
myProcess.Start();
while (!exited)
{
// This is bad bad bad bad....
}
litTest.Text = "";
foreach (string line in outputLines)
litTest.Text += line;
}
private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
outputLines.Add(outLine.Data);
}
// Handle Exited event and display process information.
private void ExitHandler(object sender, System.EventArgs e)
{
exited = true;
}
}
解决方案
I've done something very similar to your solution -- this is working fine for me:
ProcessStartInfo pInfo = new ProcessStartInfo("cmd.exe");
pInfo.FileName = exePath;
pInfo.WorkingDirectory = new FileInfo(exePath).DirectoryName;
pInfo.Arguments = args;
pInfo.CreateNoWindow = false;
pInfo.UseShellExecute = false;
pInfo.WindowStyle = ProcessWindowStyle.Normal;
pInfo.RedirectStandardOutput = true;
Process p = Process.Start(pInfo);
p.OutputDataReceived += p_OutputDataReceived;
p.BeginOutputReadLine();
p.WaitForExit();
// set status based on return code.
if (p.ExitCode == 0) this.Status = StatusEnum.CompletedSuccess;
else this.Status = StatusEnum.CompletedFailure;
The interesting differences seem to be the use of WaitForExit(), and possibly the BeginOutputReadLine().
这篇关于如何运行一个控制台应用程序,捕获输出,并在文字显示呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文