如何在进程结束之前读取C#进程的输出?
我想为python创建一个IDE,它可以像anaconda一样逐行运行。 因此,我希望在每个python命令之后返回输出 编辑:解决方案是:如何在进程结束之前读取C#进程的输出?,c#,C#,我想为python创建一个IDE,它可以像anaconda一样逐行运行。 因此,我希望在每个python命令之后返回输出 编辑:解决方案是: public Process cmd=new Process(); 公共字符串输出=”; { cmd.StartInfo.FileName=“python.exe”; cmd.StartInfo.Arguments=@“-i”;//这就是问题所在 cmd.StartInfo.RedirectStandardInput=true; cmd.StartInfo
public Process cmd=new Process();
公共字符串输出=”;
{
cmd.StartInfo.FileName=“python.exe”;
cmd.StartInfo.Arguments=@“-i”;//这就是问题所在
cmd.StartInfo.RedirectStandardInput=true;
cmd.StartInfo.RedirectStandardOutput=true;
cmd.StartInfo.UseShellExecute=false;
cmd.OutputDataReceived+=新的DataReceivedEventHandler((发送方2,e2)=>
{
如果(!String.IsNullOrEmpty(e2.Data))
{
输出+=e2.Data.ToString()+“\n”;
}
});
cmd.Start();
cmd.BeginOutputReadLine();
}
{
cmd.StandardInput.WriteLine(结果);//输出需要一些时间才能完成
}
注册到OutputDataReceived事件以获取数据:
cmd.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
// do what you need with the output (for instance append output)
}
});
在cmd.Start()之后添加:
这会导致进程读取重定向的输出
因此,最终的代码是:
Process cmd = new Process();
cmd.StartInfo.FileName = @"python.exe";
cmd.StartInfo.Arguments = @"-i";
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
// do what you need with the output (for instance append output)
cmd.StandardInput.WriteLine("python comand 1");
cmd.StandardInput.WriteLine("python comand 2");
cmd.StandardInput.WriteLine("another comand 3");
}
});
cmd.Start();
cmd.BeginOutputReadLine();
我正在运行的exe基本上有一个按钮,单击该按钮可将一行写入控制台,然后读取3行,因此事件获取数据“我的输出”,bla1、bla2和bla3获取我写入输入的值:
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("my output");
string bla = Console.ReadLine();
string bla2 = Console.ReadLine();
string bla3 = Console.ReadLine();
}
注册到OutputDataReceived事件以获取数据:
cmd.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
// do what you need with the output (for instance append output)
}
});
在cmd.Start()之后添加:
这会导致进程读取重定向的输出
因此,最终的代码是:
Process cmd = new Process();
cmd.StartInfo.FileName = @"python.exe";
cmd.StartInfo.Arguments = @"-i";
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
// do what you need with the output (for instance append output)
cmd.StandardInput.WriteLine("python comand 1");
cmd.StandardInput.WriteLine("python comand 2");
cmd.StandardInput.WriteLine("another comand 3");
}
});
cmd.Start();
cmd.BeginOutputReadLine();
我正在运行的exe基本上有一个按钮,单击该按钮可将一行写入控制台,然后读取3行,因此事件获取数据“我的输出”,bla1、bla2和bla3获取我写入输入的值:
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("my output");
string bla = Console.ReadLine();
string bla2 = Console.ReadLine();
string bla3 = Console.ReadLine();
}
请提供更多详细信息,我已尝试此解决方案,但无效。缺少另一行,我编辑了答案。我重试此解决方案,但无效。我已将问题编辑为你的答案。你能上传你的项目吗?我试过你说的,但没有结果。我应该关闭进程,以便显示输出。编辑以显示我正在运行的exe中发生的情况。。现在您的输入/输出有问题吗?请提供更多详细信息,我已尝试此解决方案,但无效。缺少另一行,我编辑了答案。我重试此解决方案,但无效。我已将问题编辑为你的答案。你能上传你的项目吗?我试过你说的,但没有结果。我应该关闭进程,以便显示输出。编辑以显示我正在运行的exe中发生的情况。。现在您是否对输入\输出或两者都有问题?