且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重定向控制台输入和输出到一个文本框

更新时间:2023-12-05 13:12:16

下面的代码是一个控制台应用程序,调用另一个控制台应用程序做一些工作,而不是一个WinForm的应用程序,但你可以很容易地更换事件处理程序(TransformProcessOutputDataReceived和TransformProcessErrorDataReceived)输出到一个文本框,而不是一个的TextWriter的。遗憾的是它并不能直接解决您的被叫控制台应用程序等待用户输入的问题。下面的代码做一些泵输入到通过标准输入被叫控制台应用程序,所以也许你可以从你的Windows应用程序以同样的方式供给。

The code below is a Console app that calls another console app to do some work and not a WinForm app, but you could easily replace the event handlers (TransformProcessOutputDataReceived, and TransformProcessErrorDataReceived) to output to a text box instead of a TextWriter. Unfortunately it doesn't directly address your issue of the called console application waiting for user input. The code below does pump some input to the called console app via standard input, so perhaps you could supply it from your windows app in the same manner.

希望这是有帮助的,我有一段时间得到它最初的工作自己赫克,对不起,我忘了我曾使用原来的引用,这是前一阵子。

Hope this was helpful, I had a heck of a time getting it to work originally myself, sorry I forgot the original references I had used, it was a while ago.

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));
        }
    }