2012-12-18 48 views
0

如果使用默認從控制檯重定向控制檯輸出重定向(不是所有的數據重定向C#)

proc = new System.Diagnostics.Process();      
proc.StartInfo.RedirectStandardOutput = true; 
proc.StartInfo.RedirectStandardInput = true; 
proc.StartInfo.RedirectStandardError = true; 
proc.StartInfo.UseShellExecute = false; 
proc.StartInfo.CreateNoWindow = true; 

proc.OutputDataReceived += proc_OutputDataReceived; 
proc.Exited += proc_Exited; 
proc.ErrorDataReceived += proc_ErrorDataReceived; 
proc.Start(); 
proc.BeginOutputReadLine(); 
proc.BeginErrorReadLine(); 

有問題(在Windows上的Git的bash msysgit測試)

  1. 在OutputDataReceived來不是所有數據。

  2. 在OutputDataReceived中沒有出現文字顏色的數據。

  3. 在ErrorDataReceived中出錯的數據。

屏幕截圖:

混帳bash的控制檯

git bash console

重定向數據

redirected data

的問題:

  1. 有辦法通過重定向獲取正確的數據嗎?

  2. 還有另一種方法從控制檯獲取正確的數據?

+0

謝謝,我不知道 「接受」 –

+0

你正在使用Unix程序的工作。這需要處理Unix終端I/O的怪癖。矩形是執行終端功能的轉義序列。底層的Unix庫有一個非常合適的名稱,它是「詛咒」。 –

+0

這很難過,但必須有解決方案 –

回答

1

我沒有找到正確的重定向控制檯輸入和輸出。

我的任務的解決方案:

  1. 運行控制檯應用程序。

    Process proc = new System.Diagnostics.Process(); 
    proc.StartInfo.FileName = "cmd.exe"; 
    proc.StartInfo.Arguments = ""; 
    proc.StartInfo.WorkingDirectory = ""; 
    proc.StartInfo.UseShellExecute = true; 
    proc.StartInfo.CreateNoWindow = false; 
    
    proc.Start(); 
    
    int count = 0; 
    //wait console 
    while (proc.MainWindowHandle == IntPtr.Zero) 
    { 
        Thread.Sleep(50); 
        count++; 
        if (count == 50) 
         return; 
    } 
    
  2. 允許從控制檯複製粘貼。

    //Attach to console 
    DllImport.ConsoleFunctions.AttachConsole(proc.Id); 
    //Get console output handle 
    IntPtr cHandleO = DllImport.GetStdHandle(-11); 
    //Set Quick edit Mode mode 
    DllImport.SetConsoleMode(cHandleO, 0x0040); 
    //Deattach from console 
    DllImport.FreeConsole(); 
    
  3. 在窗體上查看控制檯輸出。

    //Set my Panle as console window parent 
    DllImport.SetParent(proc.MainWindowHandle, panelConsole.Handle); 
    DllImport.SendMessage(proc.MainWindowHandle, 0x0112, 0xF030, 0); 
    //Resize 
    DllImport.SetWindowPos(proc.MainWindowHandle, IntPtr.Zero, 0, 0, panelConsole.Width,  
        panelConsole.Height, 0x0040); 
    //Hide border and title 
    DllImport.SetWindowLong(proc.MainWindowHandle, -16, 0x10000000 | 0x00200000); 
    
  4. 將命令從TextBox發送到控制檯。

    private DllImport.INPUT_RECORD[] PrepareData(string data) 
    { 
        DllImport.INPUT_RECORD[] rec = new DllImport.INPUT_RECORD[data.Length + 1]; 
        for (int i = 0; i < data.Length; i++) 
        { 
         rec[i].EventType = 0x0001; 
         rec[i].KeyEvent = new DllImport.KEY_EVENT_RECORD(); 
         rec[i].KeyEvent.bKeyDown = true; 
         rec[i].KeyEvent.dwControlKeyState = 0; 
         rec[i].KeyEvent.UnicodeChar = data[i]; 
         rec[i].KeyEvent.wRepeatCount = 1; 
         rec[i].KeyEvent.wVirtualKeyCode = data[i]; 
         rec[i].KeyEvent.wVirtualScanCode = (ushort) DllImport.MapVirtualKey(data[i], 0); 
        } 
        rec[data.Length].EventType = 0x0001; 
        rec[data.Length].KeyEvent = new DllImport.KEY_EVENT_RECORD(); 
        rec[data.Length].KeyEvent.bKeyDown = true; 
        rec[data.Length].KeyEvent.dwControlKeyState = 0; 
        rec[data.Length].KeyEvent.UnicodeChar = '\n'; 
        rec[data.Length].KeyEvent.wRepeatCount = 1; 
        rec[data.Length].KeyEvent.wVirtualKeyCode = '\n'; 
        rec[data.Length].KeyEvent.wVirtualScanCode = (ushort) DllImport.MapVirtualKey(0x0D, 0); 
    
        return rec; 
    } 
    
    uint count = 0; 
    //Fttach to console 
    DllImport.AttachConsole(proc.Id); 
    //Get input handle 
    IntPtr cHandleI = DllImport.GetStdHandle(-10); 
    //Prepare data 
    DllImport.INPUT_RECORD[] data = PrepareData(tb.Text); 
    //Write to console 
    DllImport.WriteConsoleInput(cHandleI, data, data.Length, out count); 
    //Deattach 
    DllImport.FreeConsole(); 
    

Use PInvoke.net for some functions

Sample project

0

StreamWriter input; 進程p = new Process(); 私人無效Form1_Load的(對象發件人,EventArgs的){

 p = new Process(); 
     p.StartInfo.FileName = ConfigurationManager.AppSettings["exe"]; 
     p.StartInfo.UseShellExecute = false;//自定義shell 
     p.StartInfo.CreateNoWindow = true;//避免顯示原始窗口 
     p.StartInfo.RedirectStandardInput = true;//重定向標準輸入(原來是CON) 
     p.StartInfo.RedirectStandardOutput = true;//重定向標準輸出 
     p.StartInfo.RedirectStandardError = true;//重定向錯誤輸出流 
     p.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived); 
     p.ErrorDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived); 

     try 
     { 
      p.Start();//GO 
      input = p.StandardInput;//重定向輸入 
      p.BeginOutputReadLine();//開始監控輸出(異步讀取) 
     } 
     catch (Win32Exception) { 
      MessageBox.Show("指定的可執行文件無效"); 
      Close(); 
     } 

    } 
+0

評論是非常*說明* :) –

+0

@ L.B你能讀嗎? –

+0

與我的代碼有什麼不同? –

相關問題