Form1.cs 3.3 KB

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