Click here to Skip to main content
12,952,575 members (41,646 online)
Rate this:
Please Sign up or sign in to vote.
See more:
(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.StartInfo.RedirectStandardError = true;
                ServerProcess.ErrorDataReceived += new DataReceivedEventHandler(ServerProcess_ErrorDataReceived);
        void ServerProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)

(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
Posted 23-Dec-12 20:44pm
Updated 24-Dec-12 8:49am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Yuki24 24-Dec-12 14:48pm
Helped me realize it used Error not output but the link didint really help me redirect the info to the richTextBox1
OriginalGriff 24-Dec-12 15:02pm
So? Is this a major problem? You know how to get teh text now, so it is a trivial task to set a Text Property, isn't it? :laugh:
Yuki24 24-Dec-12 15:23pm
my problem is i dont know how to make it autoscrool and relay the info in new lines instead of a single changign line the program you sent me didint really work i tried the code nothign jsut locks up
OriginalGriff 25-Dec-12 5:12am
Setting the text property on a new line is really not difficult...
myRichTextBox.Text += "\n" + myTextBox.Text;
And auto scroll is the RichTextBox default.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There are two approaches.
First uses events for async reading.
In addition to your code:

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

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.
Yuki24 24-Dec-12 14:56pm
much of the code you showed helped but still didint do exactly what i hoped for but thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,289
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,900

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100