下面的代碼是一個控制檯應用程序,調用另一個控制檯應用程序做了一些工作,而不是一個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));
}
}
_Possible_重複數據刪除:http://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program-c – 2010-05-21 16:59:37
是輸入發生在GUI上側,或控制檯端?也就是說,你是否有TextBox控件(例如),當輸入文本時,將文本發送到控制檯進程(Process.StandardInput.WriteLine())? – TreDubZedd 2010-05-21 17:05:22