好的,这就是我正在做的 - 我想编写一个 .net 应用程序,将标准输出/输入重定向到 Richtextbox。我已经让它工作得很好,但是一旦我将标准输入添加到混合中,我的读取命令就会冻结。这是我的表单中的相关代码。
Shell = new Process();
Shell.StartInfo.FileName = "cmd";
Shell.StartInfo.UseShellExecute = false;
Shell.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Shell.StartInfo.CreateNoWindow = true;
//Shell.StartInfo.RedirectStandardInput = true;
Shell.StartInfo.RedirectStandardOutput = true;
Shell.StartInfo.RedirectStandardError = true;
Shell.EnableRaisingEvents = true;
Shell.OutputDataReceived += new DataReceivedEventHandler(Shell_OutputDataReceived);
Shell.ErrorDataReceived += new DataReceivedEventHandler(Shell_OutputDataReceived);
Shell.Start();
Timer consoleReader = new Timer();
consoleReader.Interval = 200;
consoleReader.Tick += new EventHandler(consoleReader_Tick);
consoleReader.Start();
}
void consoleReader_Tick(object sender, EventArgs e)
{
textArea.AppendText(Shell.StandardOutput.ReadToEnd());
}
我也尝试过使用 Process 类中可用的异步读取方法来执行此操作,但是,一旦我将 standardinputredirect = true 添加到组合中,它就会在读取一行左右后挂起。
大家有什么想法吗?
[[编辑]]
好的,这是一个示例程序。我将此代码移至控制台应用程序中以稍微简化一些事情。为什么这个坏了?
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace TestAsConsoleApp
{
class Program
{
static Process Shell;
static void Main(string[] args)
{
Shell = new Process();
Shell.StartInfo.FileName = "cmd";
Shell.StartInfo.UseShellExecute = false;
Shell.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Shell.StartInfo.CreateNoWindow = true;
Shell.StartInfo.RedirectStandardInput = true;
Shell.StartInfo.RedirectStandardOutput = true;
Shell.StartInfo.RedirectStandardError = true;
Shell.Start();
Shell.EnableRaisingEvents = true;
Shell.OutputDataReceived += new DataReceivedEventHandler(Shell_OutputDataReceived);
Shell.BeginOutputReadLine();
Shell.WaitForExit();
}
static void Shell_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Console.WriteLine(e.Data);
}
}
}
修改后的程序看起来并没有被破坏。您创建一个进程请求重定向所有流。您触发创建的流程输出的异步读取。然后你就等着吧。在您的情况下,创建的 cmd.exe 不会在其输入流中获取任何内容,因此它不会产生任何输出。可以尝试下面的程序。运行它并给出一些命令,例如 dir 等,它将产生输出。希望我没有误解你的问题。
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace TestAsConsoleApp
{
class Program
{
static Process Shell;
static void Main(string[] args)
{
Shell = new Process();
Shell.StartInfo.FileName = "cmd";
Shell.StartInfo.UseShellExecute = false;
Shell.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Shell.StartInfo.CreateNoWindow = true;
Shell.StartInfo.RedirectStandardInput = true;
Shell.StartInfo.RedirectStandardOutput = true;
Shell.StartInfo.RedirectStandardError = true;
Shell.Start();
//Shell.StandardInput.WriteLine("dir");
Shell.EnableRaisingEvents = true;
Shell.OutputDataReceived += new DataReceivedEventHandler(Shell_OutputDataReceived);
Shell.BeginOutputReadLine();
//read input from your programs input and forward that to the created cmd 's input
do
{
string aLine = Console.ReadLine();
Shell.StandardInput.WriteLine(aLine);
if (aLine.ToLower() == "exit")
break;
}while(true);
Shell.WaitForExit();
}
static void Shell_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Console.WriteLine(e.Data);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)