2012-02-29 101 views
1

我寫的是通過重定向輸入和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");,我會收到「Are you sure」提示,但當我撥打inputWriter.Write("y");時,控制檯會回顯「y」,但沒有任何反應,控制檯將繼續等待輸入。 如果我打電話inputWriter.WriteLine("pause");控制檯暫停,並且在inputWriter.Write(" ");之後它繼續。

這裏有什麼問題,我該如何正確輸入重定向到控制檯和正在其內執行的應用程序(命令)兩者兼而有之?

在此先感謝。

乾杯!

回答

2

這是一個有趣的問題,它可能涉及到的win32命令解釋器會將輸入出於安全考慮的方式。有幾個項目已經解決了問題(主要是),如this one。您可能希望查看代碼(它是C++),試圖弄清楚是否有必要實施的技巧來解決輸入限制(如果它們確實是責備)。

我不認爲我見過用C#編寫一個完整的控制檯替代解決方案。

+1

最近想到的控制檯替換我見過的是POSH控制檯。是的,我不知道細節,但控制檯編程是笨重的,即使Powershell有一些限制,因爲Windows控制檯的工作方式。 – 2012-02-29 21:29:53

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"); 
     } 
    } 
+0

其實我在輸出流上使用BeginRead做了id異步。寫入輸入流會導致控制檯(或控制檯中運行的程序)等待輸入時的問題。 我可以運行諸如dir,cd等命令,但是一旦需要提示(例如y/n),控制檯就不會響應寫入輸入流的字符。 似乎我必須找到一個由控制檯執行的進程(如果有的話),然後寫入該進程的輸入流...... – 0wl 2012-02-29 23:07:13

相關問題