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

问题描述:

有Hi和提前表达感谢

我想(很难),以控制台输入和输出重定向到一个文本框。到目前为止输出工作正常,但麻烦的是与输入。
为例,我不能执行一个简单的程序,将做到以下几点:

I am trying (very hard) to redirect Console input and output into a textbox. So far output is working fine but the trouble is with input. For example I cannot execute a simple program that will do the following:

Console.WriteLine(请输入您的姓名:);
字符串名称=到Console.ReadLine();
Console.WriteLine(你好+姓名);

Console.WriteLine("Please enter your name: "); string name = Console.ReadLine(); Console.WriteLine("Hi there " + name);

我不能做到这一点是因为该方案是有原因的停止等待用户输入他/她的名字,然后按回车。如果我等待用户输入一个新的线程,然后主界面线程冻结和文本框可以永远不会收到KeyPress的。这件事情我已经完全难住了。任何意见(或更好的代码)将不胜感激。

The reason I can't achieve this is because that the program has to stop while waiting for user to type his/her name and press enter. If I wait for user input on a new thread then the main GUI thread freezes and the textbox can never receive the KeyPress. This thing has me totally stumped. Any advice (or better still code) would be greatly appreciated.

干杯

下面的代码是一个控制台应用程序,调用另一个控制台应用程序做一些工作,而不是一个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));
        }
    }