0

我试图创建一个我的世界服务器包装器,但我无法读取进程的输出。我想读取 serverProcess_ErrorDataRecevied 事件中的输出,并且我知道现在的内容不起作用。但是我可以放什么来代替阅读呢?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace MC_Server_UI
{
    public partial class Form1 : Form
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Program Files (x86)\\Java\\jre7\\bin\\java.exe", "Xmx1024M - jar " + "craftbukkit.jar" + " nogui");
        Process serverProcess;

        OpenFileDialog ofd;
        FolderBrowserDialog fbd;

        public Form1()
        {
            InitializeComponent();

            fbd = new FolderBrowserDialog();
            fbd.ShowDialog();

            startInfo.WorkingDirectory = fbd.SelectedPath;
            startInfo.RedirectStandardInput = startInfo.RedirectStandardError = true;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;

            serverProcess = new Process();
            serverProcess.StartInfo = startInfo;
            serverProcess.EnableRaisingEvents = true;
            serverProcess.ErrorDataReceived += new DataReceivedEventHandler(serverProcess_ErrorDataReceived);
            serverProcess.Exited += new EventHandler(serverProcess_Exited);
            serverProcess.Start();
        }

        private void serverProcess_ErrorDataReceived(object sender, EventArgs e)
        {
            richTextBox1.AppendText(serverProcess.StandardError.ReadToEnd());
        }

        private void serverProcess_Exited(object sender, EventArgs e)
        {

        }
    }
}
4

1 回答 1

0

看:

代码:

serverProcess.StartInfo.RedirectStandardOutput = true;
serverProcess.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
serverProcess.Start();
serverProcess.BeginOutputReadLine();

或者:

serverProcess.StartInfo.RedirectStandardOutput = true;
var output = serverProcess.StandardOutput.ReadToEnd();

另请参阅:ProcessStartInfo.RedirectStandardError

serverProcess.StartInfo.RedirectStandardError = true;
var error = serverProcess.StandardError.ReadToEnd();
于 2013-07-16T13:39:28.147 回答