2010-05-21 132 views
2

您好,感謝提前重定向控制檯輸入和輸出到一個文本框

我想(很難),以控制檯輸入和輸出重定向到一個文本框。到目前爲止輸出工作正常,但麻煩與輸入。 例如,我不能執行一個簡單的程序,將執行以下操作:

Console.WriteLine(「請輸入您的姓名:」); string name = Console.ReadLine(); Console.WriteLine(「Hi there」+ name);

我無法達到這個目的的原因是因爲程序在等待用戶鍵入他/她的名字並按下回車鍵時不得不停止。如果我等待用戶輸入新線程,則主GUI線程會凍結,並且文本框永遠不會收到KeyPress。這件事情讓我完全陷入困境。任何建議(或更好的代碼)將不勝感激。

乾杯

+1

_Possible_重複數據刪除:http://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program-c – 2010-05-21 16:59:37

+0

是輸入發生在GUI上側,或控制檯端?也就是說,你是否有TextBox控件(例如),當輸入文本時,將文本發送到控制檯進程(Process.StandardInput.WriteLine())? – TreDubZedd 2010-05-21 17:05:22

回答

0

下面的代碼是一個控制檯應用程序,調用另一個控制檯應用程序做了一些工作,而不是一個WinForm程序,但你可以很容易地更換事件處理程序(TransformProcessOutputDataReceived和TransformProcessErrorDataReceived)輸出到一個文本而不是TextWriter。不幸的是,它並不直接解決等待用戶輸入的被調用控制檯應用程序的問題。下面的代碼通過標準輸入向被調用的控制檯應用程序提供了一些輸入,所以也許你可以用同樣的方式從你的Windows應用程序提供它。

希望這是有幫助的,我有一段時間讓它工作原來我自己,對不起,我忘了我曾經使用的原始引用,這是前一段時間。

private static void ProcessRetrievedFiles(List<string> retrievedFiles) 
    { 
     Console.WriteLine(); 
     Console.WriteLine("Processing retrieved files:"); 
     Console.WriteLine("---------------------------"); 
     Console.WriteLine(); 
     foreach (string filePath in retrievedFiles) 
     { 
      if (String.IsNullOrEmpty(filePath)) continue; 

      Console.WriteLine(filePath); 
      Process transformProcess = new Process(); 

      string baseOutputFilePath = Path.Combine(ExportDirectory, Path.GetFileNameWithoutExtension(filePath)); 

      transformProcess.StartInfo.FileName = TransformerExecutablePath; 
      transformProcess.StartInfo.Arguments = string.Format(
       "-i:\"{0}\" -x:\"{1}\" -o:\"{2}.final.xml\"", 
       filePath, 
       string.Empty, 
       baseOutputFilePath); 
      transformProcess.StartInfo.UseShellExecute = false; 
      transformProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
      transformProcess.StartInfo.RedirectStandardError = true; 
      transformProcess.StartInfo.RedirectStandardOutput = true; 
      transformProcess.StartInfo.RedirectStandardInput = true; 

      transformProcess.EnableRaisingEvents = true; 

      //attach the error/output recievers for logging purposes 
      transformProcess.ErrorDataReceived += TransformProcessErrorDataReceived; 
      transformProcess.OutputDataReceived += TransformProcessOutputDataReceived; 

      ProcessBridgeFileOutputWriter = new StreamWriter(
        baseOutputFilePath + ".log", 
        false); 

      ProcessBridgeFileOutputWriter.AutoFlush = true; 

      transformProcess.Start(); 

      transformProcess.BeginOutputReadLine(); 
      transformProcess.BeginErrorReadLine(); 

      //the exe asks the user to press a key when they are done... 
      transformProcess.StandardInput.Write(Environment.NewLine); 
      transformProcess.StandardInput.Flush(); 

      //because we are not doing this asynchronously due to output writer 
      //complexities we don't want to deal with at this point, we need to 
      //wait for the process to complete 
      transformProcess.WaitForExit(); 

      ProcessBridgeFileOutputWriter.Close(); 
      ProcessBridgeFileOutputWriter.Dispose(); 

      //detach the error/output recievers 
      transformProcess.ErrorDataReceived -= TransformProcessErrorDataReceived; 
      transformProcess.OutputDataReceived -= TransformProcessOutputDataReceived; 
     } 
    } 

    static void TransformProcessOutputDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     if (!string.IsNullOrEmpty(e.Data)) 
     { 
      ProcessBridgeFileOutputWriter.WriteLine(e.Data); 
     } 
    } 

    static void TransformProcessErrorDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     if (!string.IsNullOrEmpty(e.Data)) 
     { 
      ProcessBridgeFileOutputWriter.WriteLine(string.Format("ERROR: {0}", e.Data)); 
     } 
    } 
相關問題