using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Data; using System.Drawing; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MqttMsgServerClient { public partial class Main : Form { public Main() { InitializeComponent(); } string serviceFilePath = $"{Application.StartupPath}\\MqttMsgServer.exe"; string serviceName = "MqttMsgServer"; private void btnInstall_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName); this.InstallService(serviceFilePath); this.BeginInvoke(new Action(ShowLogs),"服务安装成功!"); } private void ShowLogs(string msg) { this.richTextBox1.Text +=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"----"+ msg+"\r\n"; } private void btnStart_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName); this.BeginInvoke(new Action(ShowLogs), "服务启动!"); } private void btnStop_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName); this.BeginInvoke(new Action(ShowLogs), "服务停止!"); } private void btnUninstall_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) { this.ServiceStop(serviceName); this.UninstallService(serviceFilePath); this.BeginInvoke(new Action(ShowLogs ), "服务停止并卸载!"); } } //判断服务是否存在 private bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController sc in services) { if (sc.ServiceName.ToLower() == serviceName.ToLower()) { return true; } } return false; } //安装服务 private void InstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; IDictionary savedState = new Hashtable(); installer.Install(savedState); installer.Commit(savedState); } } //卸载服务 private void UninstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; installer.Uninstall(null); } } //启动服务 private void ServiceStart(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Stopped) { control.Start(); } } } //停止服务 private void ServiceStop(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Stop(); } } } } }