控制台输入重定向

时间:2012-02-29 21:06:05

标签: c# .net process console

我正在编写一个通过将cmd.exe的输入和输出重定向到文本框来模拟控制台的Windows应用程序。起始码如下:

StreamWriter inputWriter;
StreamReader outputReader;
StreamReader errorReader;
Process proc = new Process();
byte[] outputBuffer = new byte[1024];
byte[] errorBuffer = new byte[1024];

proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/Q";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.ErrorDialog = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
inputWriter = proc.StandardInput;
outputReader = proc.StandardOutput;
errorReader = proc.StandardError;
outputReader.BaseStream.BeginRead(outputBuffer, 0, outputBuffer.Length, ShowOutput, null);
errorReader.BaseStream.BeginRead(errorBuffer, 0, errorBuffer.Length, ShowError, null);proc.StartInfo.Arguments = "/Q";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.ErrorDialog = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
inputWriter = proc.StandardInput;
outputReader = proc.StandardOutput;
errorReader = proc.StandardError;
outputReader.BaseStream.BeginRead(outputBuffer, 0, outputBuffer.Length, ShowOutput, null);
errorReader.BaseStream.BeginRead(errorBuffer, 0, errorBuffer.Length, ShowError, null);

当我启动进程时,我能够读取控制台输出,并通过写入输入流将命令发送给它。

当我以这种方式启动某个应用程序时,该应用程序的输出也被重定向,一切仍然正常,但应用程序似乎没有接收到写入输入流的数据。甚至一些控制台命令也无法接收输入。 例如如果我呼叫inputWriter.WriteLine("del *.log");我收到“你确定”的提示,但是当我呼叫inputWriter.Write("y");时,“y”被控制台回显但没有任何反应,控制台继续等待输入。 如果我呼叫inputWriter.WriteLine("pause");控制台暂停,并在inputWriter.Write(" ");之后继续呼叫暂停。

这里有什么问题,如何正确地将输入重定向到控制台和正在其中执行的应用程序(和命令)?

提前致谢。

干杯!

2 个答案:

答案 0 :(得分:2)

这是一个有趣的问题,它可能与win32命令解释器出于安全原因处理输入的方式有关。有几个项目已经解决了问题(主要是),例如this one。你可能想查看代码(虽然它是C ++),试图弄清楚你是否需要实现一些技巧来解决输入限制(如果它们确实应该受到指责)。

我认为我从未见过用C#编写的完整控制台替换解决方案。

答案 1 :(得分:1)

我已多次这样做了。问题是因为您的程序是单线程的。您需要运行asyc进程。这是我的例子。

 private void exportButton_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process p = new System.Diagnostics.Process();

        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "";
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;

        p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(ConsoleOutputHandler);

        p.Start();
        p.BeginOutputReadLine();

        //p.WaitForExit();
        //p.Dispose();
    }

    private void UpdateTextBox(String message)
    {
        if (consoleOutputBox.InvokeRequired)
        {
            UpdateConsoleWindowDelegate update = new UpdateConsoleWindowDelegate(UpdateTextBox);
            consoleOutputBox.BeginInvoke(update, message);
        }
        else
        {
            consoleOutputBox.AppendText(message);
        }
    }

    void ConsoleOutputHandler(object sendingProcess, System.Diagnostics.DataReceivedEventArgs recieved)
    {
        if (!String.IsNullOrEmpty(recieved.Data))
        {
            UpdateTextBox(recieved.Data + "\n");
        }
    }