Hi. I am trying to open a command window and read/write it from a C# forms app. I have a textbox "textBox1" to display output and "textBox2" for me to type commands to the cmd process. This is the code:
ProcessStartInfo info = new ProcessStartInfo("cmd");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
proc = Process.Start(info);
sw = proc.StandardInput;
string buf = "";
StreamReader sr = proc.StandardOutput;
while (serverrunning) {
buf = sr.ReadLine();
textBox1.Text += buf + Environment.NewLine;
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
Application.DoEvents();
}
This works for regular DOS stuff. When I run it, I get the "Microsoft Windows [Version 6.1.7600], Copyright" etc etc command prompt header. Only issue is I am not receiving the last line of text until I send a CR, I assume because ReadLine is waiting for that to flush its buffer. I can live with that, but then I launch my java app and that is where the real problem starts.
The java app is a Minecraft server. When you fire it up (java -Xms1G -Xmx1G craftbukkit.jar) regularly I get this:
16:42:46 [INFO] Starting minecraft server version Beta 1.4
16:42:46 [INFO] Loading properties
16:42:46 [INFO] Starting Minecraft server on *:25565
16:42:46 [INFO] This server is running Craftbukkit version git-Bukkit-0.0.0-646-gb61ef8c-b670jnks (M
C: 1.4)
16:42:46 [INFO] Preparing level "world"
16:42:46 [INFO] Preparing start region
16:42:46 [INFO] 144 recipes
16:42:47 [INFO] Preparing spawn area: 89%
16:42:47 [INFO] Done (0.117s)! For help, type "help" or "?[/code]
Problem is, when I run it from my C# linked command process I am seeing this in my textbox:
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
c:\dropbox\projects\MinecraftManager\bin\Debug>cd\applications\minecraft\server
c:\Applications\Minecraft\Server>java -Xms1G -Xmx1G craftbukkit-0.0.1-SNAPSHOT.jar
>
>
>
>
>
>
>
>
>
I guess java doesn't do stdout right or something. Is there a way around this?
Loading
Sam HobbsPosted Apr 8, 2011, 5:55 PM
Sam HobbsPosted Apr 8, 2011, 6:54 PM
VulpesPosted Apr 8, 2011, 6:37 PM
"stop" + (char)13 + (char)10
KenPosted Apr 8, 2011, 6:13 PM
Next problem, hehe, is that the java app doesn't accept StandardInput. I sent "stop" + (char)13 to StandardInput and nothing happened, it didn't process it. Is there some other trick for that? this is my sending code that I bound to a KeyPress event on the textbox I type input in:
void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
if (e.KeyChar == (char)13) {
sw.Write(textBox2.Text);
sw.Flush();
textBox2.Text = ""; textBox2.Focus();
e.Handled = true;
label3.Text = "SENT";
} else {
if (label3.Text != "") label3.Text = "";
}
}
I've also tried using WriteLine to no avail.