我正在编写一个windows应用程序,它通过将cmd.exe的输入和输出重定向到文本框来模拟控制台。起始代码如下:
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(" ");之后,它会像往常一样继续。
这里的问题是什么?我如何正确地将输入重定向到控制台和在其中执行的应用程序(和命令)?
提前谢谢。
干杯!
发布于 2012-03-01 05:12:29
这是一个有趣的问题,它可能与win32命令解释器出于安全原因处理输入的方式有关。有几个项目(大部分)已经解决了这些问题,比如this one。您可能希望查看代码(尽管它是C++ ),以找出是否需要实现一些技巧来绕过输入限制(如果它们确实是罪魁祸首)。
我想我从来没有见过用C#写的完整的控制台替换解决方案。
发布于 2012-03-01 05:14:30
我已经在很多场合这样做了。这个问题是因为你的程序是单线程的。您需要运行进程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");
}
}https://stackoverflow.com/questions/9506606
复制相似问题