我正在尝试创建一个简单的winform,以在远程计算机上运行Life is Feudal服务器可执行文件。在服务器上的cmd中启动服务器的命令是"C:\Program Files (x86)\Steam\life is feudal\ddctd_cm_yo_server.exe" WorldDS 1
目前,我正在尝试为其创建一个简单的开始/停止按钮。我可以弄清楚以后如何停止它,我只想尝试使“开始”部分起作用。我不太确定从哪里开始代码。我现在所拥有的按钮是:'''
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";
}
}
}
}
'''我想添加一个click事件,当按钮显示“开始”时,该事件在第一次单击时以'WorldDS1'参数启动可执行文件。然后,我想添加一个单击事件,以在按钮显示“停止”时关闭第二次单击的过程。我只是从C#开始使用Visual Studios,所以越简单越好,任何帮助将不胜感激。
如果该程序已安装在PC上并想要启动它,则可以执行以下操作。首先,您必须知道可执行文件和文件名的位置。
//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;
}