记录c#开发Windows服务的过程

1.新建Windows服务项目

 2.重命名Service1.cs

 3.添加安装程序

 4.修改service,修改信息如下:

备注:ServiceName和安装后的服务名保持一致哦!!!

 5.修改serviceProcessInstaller1

 6.编辑TransService代码

 

7.添加新项目窗体应用程序

 

 

 代码:

  1 using System;
  2 using System.Windows.Forms;
  3 using System.ServiceProcess;
  4 using System.Configuration.Install;
  5 using System.Collections;
  6 
  7 namespace TransServiceTool
  8 {
  9     public partial class Form1 : Form
 10     {
 11         public Form1()
 12         {
 13             InitializeComponent();
 14         }
 15         string serviceFilePath = $"{Application.StartupPath}\\TransService.exe";
 16         string serviceName = "TransService";
 17 
 18         private void Form1_Load(object sender, EventArgs e)
 19         {
 20             if (this.IsServiceExisted(serviceName))
 21             {
 22                 this.btn_install.Enabled = false;
 23             }
 24             else
 25             {
 26                 this.btn_uninstall.Enabled = false;
 27             }
 28 
 29             ServiceControllerStatus serviceControllerStatus = this.IsServiceStatus(serviceName);
 30             if (serviceControllerStatus == 0)
 31             {
 32                 this.btn_start.Enabled = false;
 33                 this.btn_stop.Enabled = false;
 34             }
 35             if (serviceControllerStatus.Equals(ServiceControllerStatus.Stopped))
 36             {
 37                 this.btn_start.Enabled = true;
 38                 this.btn_stop.Enabled = false;
 39             }
 40             if (serviceControllerStatus.Equals(ServiceControllerStatus.Running))
 41             {
 42                 this.btn_start.Enabled = true;
 43                 this.btn_stop.Enabled = false;
 44             }
 45         }
 46 
 47         private void btn_install_Click(object sender, EventArgs e)
 48         {
 49             if (!this.IsServiceExisted(serviceName))
 50             {
 51                 this.InstallService(serviceFilePath);
 52                 this.btn_install.Enabled = false;
 53                 this.btn_uninstall.Enabled = true;
 54                 this.btn_start.Enabled = true;
 55                 this.btn_stop.Enabled = false;
 56             }
 57         }
 58 
 59         // 启动服务
 60         private void btn_start_Click(object sender, EventArgs e)
 61         {
 62             ServiceControllerStatus serviceControllerStatus = this.IsServiceStatus(serviceName);
 63             if (serviceControllerStatus.Equals(ServiceControllerStatus.Stopped))
 64             {
 65                 this.ServiceStart(serviceName);
 66                 this.btn_start.Enabled = false;
 67                 this.btn_stop.Enabled = true;
 68             }
 69             else
 70             {
 71                 //服务停止中,请稍后重试!
 72             }
 73         }
 74 
 75         // 停止服务
 76         private void btn_stop_Click(object sender, EventArgs e)
 77         {
 78             ServiceControllerStatus serviceControllerStatus = this.IsServiceStatus(serviceName);
 79             if (serviceControllerStatus.Equals(ServiceControllerStatus.Running))
 80             {
 81                 this.ServiceStop(serviceName);
 82                 this.btn_start.Enabled = true;
 83                 this.btn_stop.Enabled = false;
 84             }
 85             else
 86             {
 87                 //服务启动中,请稍后重试!
 88             }
 89         }
 90 
 91         // 卸载服务
 92         private void btn_uninstall_Click(object sender, EventArgs e)
 93         {
 94             if (this.IsServiceExisted(serviceName))
 95             {
 96                 ServiceControllerStatus serviceControllerStatus = this.IsServiceStatus(serviceName);
 97                 if (serviceControllerStatus.Equals(ServiceControllerStatus.Running))
 98                 {
 99                     this.ServiceStop(serviceName);
100                 }
101                 this.UninstallService(serviceFilePath);
102                 this.btn_uninstall.Enabled = false;
103                 this.btn_install.Enabled = true;
104                 this.btn_start.Enabled = false;
105                 this.btn_stop.Enabled = false;
106             }
107         }
108 
109         //判断服务是否存在
110         private bool IsServiceExisted(string serviceName)
111         {
112             ServiceController[] services = ServiceController.GetServices();
113             foreach (ServiceController sc in services)
114             {
115                 if (sc.ServiceName.ToLower() == serviceName.ToLower())
116                 {
117                     return true;
118                 }
119             }
120             return false;
121         }
122 
123         //判断服务是否是停止状态
124         private ServiceControllerStatus IsServiceStatus(string serviceName)
125         {
126             ServiceController[] services = ServiceController.GetServices();
127             foreach (ServiceController sc in services)
128             {
129                 if (sc.ServiceName.ToLower() == serviceName.ToLower())
130                 {
131                     return sc.Status;
132                 }
133             }
134             return new ServiceControllerStatus();
135         }
136         //安装服务
137         private void InstallService(string serviceFilePath)
138         {
139             using (AssemblyInstaller installer = new AssemblyInstaller())
140             {
141                 installer.UseNewContext = true;
142                 installer.Path = serviceFilePath;
143                 IDictionary savedState = new Hashtable();
144                 installer.Install(savedState);
145                 installer.Commit(savedState);
146             }
147         }
148         //卸载服务
149         private void UninstallService(string serviceFilePath)
150         {
151             using (AssemblyInstaller installer = new AssemblyInstaller())
152             {
153                 installer.UseNewContext = true;
154                 installer.Path = serviceFilePath;
155                 installer.Uninstall(null);
156             }
157         }
158         //启动服务
159         private void ServiceStart(string serviceName)
160         {
161             using (ServiceController control = new ServiceController(serviceName))
162             {
163                 if (control.Status == ServiceControllerStatus.Stopped)
164                 {
165                     control.Start();
166                 }
167             }
168         }
169         //停止服务
170         private void ServiceStop(string serviceName)
171         {
172             using (ServiceController control = new ServiceController(serviceName))
173             {
174                 if (control.Status == ServiceControllerStatus.Running)
175                 {
176                     control.Stop();
177                 }
178             }
179         }
180     }
181 }

 

posted @ 2024-06-27 20:01  快乐的小小码农  阅读(13)  评论(0编辑  收藏  举报