在重定向控制台输入和输出方面需要帮助 [英] Need help with Redirecting console input and output

查看:59
本文介绍了在重定向控制台输入和输出方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(2012年12月24日更新)

到目前为止这是我的代码,因为解决方案1和解决方案2对我来说很有帮助

(updated 12/24/2012)
So far this is my code after solution 1 and 2 were helpfull by didint intirly work for me

private void button1_Click(object sender, EventArgs e)
        { 
            {
                Process ServerProcess = new Process();
                ServerProcess.StartInfo.FileName = "CMD.exe";
                ServerProcess.StartInfo.Arguments = "/c java -jar Server.jar";
                ServerProcess.StartInfo.CreateNoWindow = true;
                ServerProcess.StartInfo.UseShellExecute = false;
                ServerProcess.StartInfo.RedirectStandardError = true;
                ServerProcess.StartInfo.RedirectStandardOutput = false;
                ServerProcess.StartInfo.RedirectStandardInput = true;
                ServerProcess.Start();
                ServerProcess.BeginErrorReadLine();
                ServerProcess.WaitForExit();
                ServerProcess.StartInfo.RedirectStandardError = true;

                ServerProcess.ErrorDataReceived += new DataReceivedEventHandler(ServerProcess_ErrorDataReceived);
            }  
        } 
        void ServerProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {

        }



(首发信息)

我一直在努力尝试找出如何核心重定向输出和输入。

表单将有一个richtextbox1用于输出,textbox1用于向控制台发送输入,button1用于启动服务器。这是一个Minecraft服务器btw。

所以我希望输出为每一个出来的东西创建新行,这样我就能看到服务器上发生了什么。

if任何人都知道我怎么能去abotu这将是非常好的。

(新信息)

程序确实使用错误不输出所以我希望将其重定向到richTextBox1 in实时所有我有gotton到目前为止是一行与soem不同的代码,但我希望它为每个条目填充新行和自动滚动


(First Post info)
I have been having troubles trying to find out how to corectly redirect the output and input.
the form will have a richtextbox1 for the output and a textbox1 to send input to the console and the button1 to start the server. this is a minecraft server btw.
so i want the output to creat new lines for every thing that comes out so i can see whats going on with the server.
if anyone knows how I could go abotu this I would be greatfull.
(New Info)
the program does use error not output so i want this to be redirected to richTextBox1 in realtime all i have gotton so far was one line with soem different code but i want it to fill new lines for every entry and auto scroll

推荐答案

还有StandardError你可能想要重定向 - 一些应用程序也使用它。



有一篇文章显示要处理这三个:如何重定向应用程序的标准输入/输出 [ ^ ]可能会有所帮助。
There is also the StandardError you may want to redirect - some applications use it as well.

There is an article showing to do deal with all three here: How to redirect Standard Input/Output of an application[^] which may be of help.


有两种方法。

首先使用事件进行异步读取。

除了你的代码:

There are two approaches.
First uses events for async reading.
In addition to your code:
ServerProcess.BeginOutputReadLine();



you ''首先需要附加事件处理程序:


you''ll need to first attach event handlers:

ServerProcess.OutputDataReceived += new DataReceivedEventHandler(ServerProcess_OutputDataReceived);



和处理程序应该是这样的:


and handler should be like:

void ServerProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (!string.IsNullOrEmpty(e.Data))
    {
        UpdateTextBox(e.Data);
    }
}



当流程输出带换行符的文本数据时,这种方法很有效( Console.Out.WriteLine() )并按常规方式刷新输出流( Console.Out.Flush())。

对于每个WriteLine,您的处理程序将被调用。

如果进程只使用Write(),那么在输出流关闭之前你的处理程序将不会被调用,你将立即得到所有数据。



第二种方法不是使用 BeginOutputReadLine(),而是设置自己的线程或BackgroungWorker来异步读取数据。设置起来有点困难,但你不依赖换行,你甚至可以用它来传输二进制数据。


This works well when process outputs text data with line feeds (Console.Out.WriteLine()) and flushes output stream regulary (Console.Out.Flush()).
For each WriteLine your handler will be called.
If process uses only Write() your handler will not be called until output stream is closed and you''ll get all data at once.

Second approach is not to use BeginOutputReadLine() but setup your own thread or BackgroungWorker to read data asynchronously as it comes. It''s a bit harder to setup, but you don''t depend on line feeds and you can even use it to transfer binary data.


这篇关于在重定向控制台输入和输出方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆