Form1.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration.Install;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.ServiceProcess;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace MqttMsgServerClient
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. string serviceFilePath = $"{Application.StartupPath}\\MqttMsgServer.exe";
  22. string serviceName = "MqttMsgServer";
  23. private void btnInstall_Click(object sender, EventArgs e)
  24. {
  25. if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
  26. this.InstallService(serviceFilePath);
  27. this.BeginInvoke(new Action<string>(ShowLogs),"服务安装成功!");
  28. }
  29. private void ShowLogs(string msg)
  30. {
  31. this.richTextBox1.Text +=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"----"+ msg+"\r\n";
  32. }
  33. private void btnStart_Click(object sender, EventArgs e)
  34. {
  35. if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
  36. this.BeginInvoke(new Action<string>(ShowLogs), "服务启动!");
  37. }
  38. private void btnStop_Click(object sender, EventArgs e)
  39. {
  40. if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
  41. this.BeginInvoke(new Action<string>(ShowLogs), "服务停止!");
  42. }
  43. private void btnUninstall_Click(object sender, EventArgs e)
  44. {
  45. if (this.IsServiceExisted(serviceName))
  46. {
  47. this.ServiceStop(serviceName);
  48. this.UninstallService(serviceFilePath);
  49. this.BeginInvoke(new Action<string>(ShowLogs ), "服务停止并卸载!");
  50. }
  51. }
  52. //判断服务是否存在
  53. private bool IsServiceExisted(string serviceName)
  54. {
  55. ServiceController[] services = ServiceController.GetServices();
  56. foreach (ServiceController sc in services)
  57. {
  58. if (sc.ServiceName.ToLower() == serviceName.ToLower())
  59. {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. //安装服务
  66. private void InstallService(string serviceFilePath)
  67. {
  68. using (AssemblyInstaller installer = new AssemblyInstaller())
  69. {
  70. installer.UseNewContext = true;
  71. installer.Path = serviceFilePath;
  72. IDictionary savedState = new Hashtable();
  73. installer.Install(savedState);
  74. installer.Commit(savedState);
  75. }
  76. }
  77. //卸载服务
  78. private void UninstallService(string serviceFilePath)
  79. {
  80. using (AssemblyInstaller installer = new AssemblyInstaller())
  81. {
  82. installer.UseNewContext = true;
  83. installer.Path = serviceFilePath;
  84. installer.Uninstall(null);
  85. }
  86. }
  87. //启动服务
  88. private void ServiceStart(string serviceName)
  89. {
  90. using (ServiceController control = new ServiceController(serviceName))
  91. {
  92. if (control.Status == ServiceControllerStatus.Stopped)
  93. {
  94. control.Start();
  95. }
  96. }
  97. }
  98. //停止服务
  99. private void ServiceStop(string serviceName)
  100. {
  101. using (ServiceController control = new ServiceController(serviceName))
  102. {
  103. if (control.Status == ServiceControllerStatus.Running)
  104. {
  105. control.Stop();
  106. }
  107. }
  108. }
  109. }
  110. }