Form1.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 WebScannerClient
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. string serviceFilePath = $"{Application.StartupPath}\\WebScanner.exe";
  22. string serviceName = "WebScanner";
  23. private void btnInstall_Click(object sender, EventArgs e)
  24. {
  25. if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
  26. this.InstallService(serviceFilePath);
  27. }
  28. private void btnStart_Click(object sender, EventArgs e)
  29. {
  30. if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
  31. }
  32. private void btnStop_Click(object sender, EventArgs e)
  33. {
  34. if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
  35. }
  36. private void btnUninstall_Click(object sender, EventArgs e)
  37. {
  38. if (this.IsServiceExisted(serviceName))
  39. {
  40. this.ServiceStop(serviceName);
  41. this.UninstallService(serviceFilePath);
  42. }
  43. }
  44. //判断服务是否存在
  45. private bool IsServiceExisted(string serviceName)
  46. {
  47. ServiceController[] services = ServiceController.GetServices();
  48. foreach (ServiceController sc in services)
  49. {
  50. if (sc.ServiceName.ToLower() == serviceName.ToLower())
  51. {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. //安装服务
  58. private void InstallService(string serviceFilePath)
  59. {
  60. using (AssemblyInstaller installer = new AssemblyInstaller())
  61. {
  62. installer.UseNewContext = true;
  63. installer.Path = serviceFilePath;
  64. IDictionary savedState = new Hashtable();
  65. installer.Install(savedState);
  66. installer.Commit(savedState);
  67. }
  68. }
  69. //卸载服务
  70. private void UninstallService(string serviceFilePath)
  71. {
  72. using (AssemblyInstaller installer = new AssemblyInstaller())
  73. {
  74. installer.UseNewContext = true;
  75. installer.Path = serviceFilePath;
  76. installer.Uninstall(null);
  77. }
  78. }
  79. //启动服务
  80. private void ServiceStart(string serviceName)
  81. {
  82. using (ServiceController control = new ServiceController(serviceName))
  83. {
  84. if (control.Status == ServiceControllerStatus.Stopped)
  85. {
  86. control.Start();
  87. }
  88. }
  89. }
  90. //停止服务
  91. private void ServiceStop(string serviceName)
  92. {
  93. using (ServiceController control = new ServiceController(serviceName))
  94. {
  95. if (control.Status == ServiceControllerStatus.Running)
  96. {
  97. control.Stop();
  98. }
  99. }
  100. }
  101. }
  102. }