C# | Developing GUI for server?

Discussion in 'Programming' started by Qruet, May 10, 2017.

  1. Alright, as a plugin developer I use a private server hosted off my laptop in order to test my plugins. I decided to get creative and create a gui with options to manage my server with buttons rather then just running commands. This is just a little personal project of mine however with my start button, it's suppose to run the spigot server jar however it does run about after the message saying ***Server will start in 15 seconds*** nothing happens and then suddenly closes. I'm not sure why and I'm hoping I can get some help on this issue.
    Thanks!

    Code (Text):
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace ServerLauncher
    {
        public partial class Form1 : Form
        {

            Process process;
            public Form1()
            {
                InitializeComponent();
                button1.Text = "START";
                button1.BackColor = Color.Green;

                button2.Text = "STOP";
                button2.BackColor = Color.Red;

                ReadIp();

            }

            private void button1_Click(object sender, EventArgs e)
            {
                string pathToJavaApp = @"C:\Users\austi\Documents\PS\spigot-1.11.2-R0.1-SNAPSHOT.jar";
                var startInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = false,
                    Arguments = "/c java -Xms1024m -Xmx4095m -jar \"" + pathToJavaApp + ""
                };

                process = new Process {
                    StartInfo = startInfo
                };

                process.Start();

                string standard_output;
                /*while ((standard_output = process.StandardOutput.ReadLine()) != null)
                {
                    richTextBox1.Text = process.StandardOutput.ReadLine();
                }*/


                //process.WaitForExit();


            }

            private void button2_Click(object sender, EventArgs e)
            {
                process.StandardInput.WriteLine("stop");
            }

            private void ReadIp()
            {
                var startInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = false
                };

                var process1 = new Process { StartInfo = startInfo };

                process1.Start();
                process1.StandardInput.WriteLine("ipconfig");

                string standard_output;
                while ((standard_output = process1.StandardOutput.ReadLine()) != null)
                {
                    if (standard_output.Contains("Link-local IPv6 Address"))
                    {
                        label1.Text = ("IP Address: " + process1.StandardOutput.ReadLine().Replace("IPv4 Address. . . . . . . . . . . :", "") + ":25565");
                        break;
                    }
                }



                process1.StandardInput.WriteLine("exit");
                process1.WaitForExit();
            }
        }
    }
     
     
  2. It probably cannot find the server.properties file. You need to set the working directory of the process to the server's directory, where the server.properties file exists.


    Code (Text):
    string pathToJavaApp = @"spigot-1.11.2-R0.1-SNAPSHOT.jar";
    var startInfo = new ProcessStartInfo
    {
        WorkingDirectory = @"C:\Users\austi\Documents\PS\",
        ...
    }

    ...
    You would have seen this if you had properly redirected the process output and written it out to the GUI.
    You almost did that correctly, but your code would replace the previous line of output with the new line, so you'd always get to see only the last line in the rich text box.
     
    • Like Like x 1