来自 Windows 个表单控件的控制台输入
Console Input from Windows Form Controls
我正在创建一个应用程序,它从控制台接收输出并使用 windows 表单通过列表框将其显示在对用户更友好的视图中。我通过启动 .jar 文件的进程并使用以下代码来完成此操作:
public void DataReceived(object sender, DataReceivedEventArgs e)
{
// e.Data is the line which was written to standard output
if (e.Data != null)
{
// textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = textBox1.Text + e.Data; });
Invoke(new MethodInvoker(delegate { listBox1.Items.Add(e.Data); }));
}
}
public void StartServer()
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo("java", @"-Xmx1024M -jar " + UltimateMinecraftServerCreator.Properties.Settings.Default.jarname);
p.StartInfo.WorkingDirectory = UltimateMinecraftServerCreator.Properties.Settings.Default.jarlocator;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += DataReceived;
p.Start();
p.BeginOutputReadLine();
}
我想知道是否可以在同一个表单上允许用户单击将命令发送到控制台的按钮,以及他们是否能够键入知道的命令。
谢谢!
终于搞定了!真的很简单,我将 Process p 的声明移到所有控件代码之上,因此它变得通用并且能够使用 p.StandardInput.WriteLine("");
我正在创建一个应用程序,它从控制台接收输出并使用 windows 表单通过列表框将其显示在对用户更友好的视图中。我通过启动 .jar 文件的进程并使用以下代码来完成此操作:
public void DataReceived(object sender, DataReceivedEventArgs e)
{
// e.Data is the line which was written to standard output
if (e.Data != null)
{
// textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = textBox1.Text + e.Data; });
Invoke(new MethodInvoker(delegate { listBox1.Items.Add(e.Data); }));
}
}
public void StartServer()
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo("java", @"-Xmx1024M -jar " + UltimateMinecraftServerCreator.Properties.Settings.Default.jarname);
p.StartInfo.WorkingDirectory = UltimateMinecraftServerCreator.Properties.Settings.Default.jarlocator;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += DataReceived;
p.Start();
p.BeginOutputReadLine();
}
我想知道是否可以在同一个表单上允许用户单击将命令发送到控制台的按钮,以及他们是否能够键入知道的命令。
谢谢!
终于搞定了!真的很简单,我将 Process p 的声明移到所有控件代码之上,因此它变得通用并且能够使用 p.StandardInput.WriteLine("");