0

I'm trying to create a simple winform to run the Life is Feudal server executable on a remote machine. The command to start the server in cmd on the server is "C:\Program Files (x86)\Steam\life is feudal\ddctd_cm_yo_server.exe" WorldDS 1

For now, I'm trying to make a simple start/stop button for it. I can figure out how to stop it later, I just want to try and get the "start" part working. I'm not quite sure where to begin with the code. All I have for the button now is: '''

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LifeIsFeudalServerManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Serverstatus_Click(object sender, EventArgs e)
        {

        }
        bool servcrtl = true;
        private void ServerControl_Click(object sender, EventArgs e)
        {
            if (servcrtl == true)
            {
                servcrtl = false;
                ServerControl.Text = "Stop";


            }
            else
            {
                servcrtl = true;
                ServerControl.Text = "Start";


            }
        }

    }
}

''' I'd like to add in a click event that starts the executable with the 'WorldDS1' argument on the first click, when button is displaying "start". I'd then like to add a click event that closes the process on the second click when button is displaying "stop". I'm just starting out in c# and using Visual Studios, so the simpler the better, and any help would be greatly appreciated.

JAB
  • 1
  • Look into PSExec here https://learn.microsoft.com/en-us/sysinternals/downloads/psexec – bic Jan 05 '20 at 00:50
  • You can refer to bic;s advice. Also, you can look at [Execute exe on remote machine](https://stackoverflow.com/questions/25782308/execute-exe-on-remote-machine) to know how to use it. – Jack J Jun Jan 06 '20 at 07:19

1 Answers1

0

You can do something like this if the program is installed on your PC and you want to start it. First, you have to know the location of the executable file and the filename.

//Pass your command string
private List<string> ExecuteCommand(string Command)
        {
            string cmdString = "C:\Program Files (x86)\Steam\life is feudal\ddctd_cm_yo_server.exe"

            List<string> CommandLineResponse = new List<string>();
            Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";//the process to execute
            cmd.StartInfo.RedirectStandardInput = true;//this is set to be able to pass in string arguments to command-line
            cmd.StartInfo.RedirectStandardOutput = true;//this is set to true to get the response at the command-line
            cmd.StartInfo.CreateNoWindow = true;//do not show a command line window
            cmd.StartInfo.UseShellExecute = false;
            cmd.Start();//start the process

            cmd.StandardInput.WriteLine(cmdString);
            cmd.StandardInput.Flush();
            cmd.StandardInput.Close();
            while (!(cmd.StandardOutput.EndOfStream))
            {
                CommandLineResponse.Add(cmd.StandardOutput.ReadLine());
            }
            return CommandLineResponse;
        }
Formula12
  • 305
  • 1
  • 3
  • 14