C# 过程不';开始

C# 过程不';开始,c#,process,C#,Process,我们正试图开始一个过程,但它就是没有开始。当然,有更多的方法来更改currentIndex等,但如果我们添加它们,将有更多的代码 public class Server { public bool isRunning { get; set; } private Process p; public string Name { get; set; } public string Path { get; set; } public string Output

我们正试图开始一个过程,但它就是没有开始。当然,有更多的方法来更改currentIndex等,但如果我们添加它们,将有更多的代码

public class Server
{
    public bool isRunning { get; set; }
    private Process p;
    public string Name { get; set; }
    public string Path { get; set; }

    public string Output { get; private set; }

    public event DataReceivedEventHandler OutputDataReceived;
    public event DataReceivedEventHandler ErrorDataReceived;

    protected virtual void OnReceived(object sender, DataReceivedEventArgs e)
    {
        if (OutputDataReceived != null)
        {
            OutputDataReceived(Name, e);
            Output += e.Data + "\n";
        }
    }
    protected virtual void OnError(object sender, DataReceivedEventArgs e)
    {
        if (ErrorDataReceived != null)
        {
            ErrorDataReceived(Name, e);
            Output += e.Data + "\n";
        }
    }

    public Server(string name, string path)
    {
        p = new Process();
        this.Name = name;
        this.Path = path;

        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardInput = true;

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.FileName = @"C:\Program Files (x86)\Java\jre1.8.0_25\bin\java.exe";

        p.OutputDataReceived += OnReceived;
        p.ErrorDataReceived += OnError;
    }

    public bool Start()
    {
        try
        {
            if (isRunning == false)
            {
                p.StartInfo.WorkingDirectory = Path;
                p.StartInfo.Arguments = @"-Xmx1024M -jar " + System.IO.Path.GetFileName(getJarFile()) + " -nojline";

                bool b = p.Start();
                if (b == false) throw new ApplicationException();
                p.BeginOutputReadLine();

                isRunning = true;
                return true;
            }
            else { return false; }
        }

        catch (Exception ex) { System.Windows.Forms.MessageBox.Show("An error occured while starting the server: " + ex, "Error"); return false; }
    }

    public bool Stop()
    {
        try
        {
            if (isRunning == true)
            {
                using (System.IO.StreamWriter sw = p.StandardInput)
                {
                    sw.WriteLine("/stop");
                    isRunning = false;
                }

                return true;
            }

            else { return false; }
        }

        catch (Exception ex) { System.Windows.Forms.MessageBox.Show("An error occured while stopping the server: " + ex, "Error"); return false; }
    }

    private string getJarFile()
    {
        string path = "ERROR";

        Console.WriteLine(Path);

        foreach (string fp in System.IO.Directory.GetFiles(Path))
            if (fp.EndsWith(".jar"))
                path = fp;

        Console.WriteLine(path);
        return path == "ERROR" ? null : path;
    }
}
表格类别:

public partial class MainForm : Form
{
    public List<Server> servers = new List<Server>();
    public int currentIndex = -1;

    delegate void AddConsoleTextCallback(string text);

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (string path in Directory.GetDirectories(Program.programPath + "\\Servers"))
            servers.Add(new Server(Path.GetFileName(path), path));
        foreach (Server s in servers)
            serversComboBox.Items.Add(s.Name + " (Idle)");
        foreach (Server s in servers)
        {
            s.OutputDataReceived += s_DataReceived;
            s.ErrorDataReceived += s_DataReceived;
        }
    }

    private void AddText(string text)
    {
        if (this.consoleTextBox.InvokeRequired)
        {
            AddConsoleTextCallback d = new AddConsoleTextCallback(AddText);
            this.Invoke(d, text);
        }
        else
        {
            this.consoleTextBox.Text += text;
        }
    }

    void s_DataReceived(object sender, DataReceivedEventArgs e)
    {
        if ((string)sender == servers[currentIndex].Name)
            this.AddText(e.Data + "\n");
    }

    private void button1_Click(object sender, EventArgs e) // Start
    {
        if (currentIndex == -1)
        {
            MessageBox.Show("No server is selected!", "Error");
            return;
        }

        try 
        { 
            if (servers[currentIndex].Start())
            {
                startButton.Enabled = false;
                if (((string)serversComboBox.Items[currentIndex]).Contains(" (Idle)"))
                    serversComboBox.Items[currentIndex] = 
                        ((string)serversComboBox.Items[currentIndex]).Replace(" (Idle)", " (Running)");
                else if (((string)serversComboBox.Items[currentIndex]).Contains(" (Running)"))
                    serversComboBox.Items[currentIndex] =
                        ((string)serversComboBox.Items[currentIndex]).Replace(" (Running)", " (Idle)");
            }
        }
        catch (Exception ex) { MessageBox.Show("An error occured: " + ex); }
    }
}
public分部类MainForm:Form
{
公共列表服务器=新列表();
公共int currentIndex=-1;
委托无效AddConsoleTextCallback(字符串文本);
私有void Form1\u加载(对象发送方、事件参数e)
{
foreach(Directory.GetDirectories(Program.programPath+“\\Servers”)中的字符串路径)
添加(新服务器(Path.GetFileName(Path),Path));
foreach(服务器中的服务器)
serversComboBox.Items.Add(s.Name+“(Idle)”);
foreach(服务器中的服务器)
{
s、 OutputDataReceived+=s_DataReceived;
s、 ErrorDataReceived+=s_DataReceived;
}
}
私有void AddText(字符串文本)
{
if(this.consoleTextBox.invokererequired)
{
AddConsoleExtCallback d=新的AddConsoleExtCallback(AddText);
调用(d,文本);
}
其他的
{
this.consoleTextBox.Text+=文本;
}
}
无效s_DataReceived(对象发送方,DataReceivedEventArgs e)
{
如果((字符串)发送方==服务器[currentIndex].Name)
此.AddText(e.Data+“\n”);
}
私有无效按钮1\u单击(对象发送者,事件参数)//开始
{
如果(当前索引==-1)
{
Show(“未选择服务器!”,“错误”);
返回;
}
尝试
{ 
如果(服务器[currentIndex].Start())
{
startButton.Enabled=false;
如果(((字符串)serversComboBox.Items[currentIndex])包含(“(空闲)”)
serversComboBox.Items[currentIndex]=
((字符串)serversComboBox.Items[currentIndex]).Replace(((Idle)”,“(Running)”);
else if(((字符串)serversComboBox.Items[currentIndex])包含(((正在运行)”)
serversComboBox.Items[currentIndex]=
((字符串)serversComboBox.Items[currentIndex]).Replace(“(正在运行)”,“(空闲)”;
}
}
catch(Exception ex){MessageBox.Show(“发生错误:“+ex”);}
}
}
另外,这可能是一个愚蠢的错误,MainForm类中的委托直接来自互联网,因此我们不知道是否有必要这样做。不过,我还是非常感谢你的帮助

编辑:


我们已经解决了部分问题:p.StartInfo.WorkingDirectory。但是我们现在有另一个错误:.jar只是显示(如表单中所示,这不应该发生),并且它在.jar本身中生成了很多错误(要列出的错误太多了)。

我猜失败的文件位于带有空格的路径中。替换
path=fp带有
path=“\”“+fp+”\”

请提供答案,这将大大增加您获得帮助的机会。猜测不是提供好答案的最佳方法。您应该首先询问OP文件路径是否包含空格。是,路径可能包含空格:)