使用C#编写的Windows服务程序,在Winform中进行调用。
常用工具类方法检测服务是否存在或者安装,获取服务状态,启动服务,停止服务的方法。
以在Winform中重启服务为例。
注:
博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载
新建工具类WinServiceHelper
检测服务是否安装或者存在的方法
/// <summary> ///服务是否安装/存在 /// </summary> /// <param name="serviceName">服务名</param> /// <returns></returns> public static bool IsServiceInstalled(stringserviceName) { bool exists = false; System.ServiceProcess.ServiceController[] services =System.ServiceProcess.ServiceController.GetServices(); foreach (System.ServiceProcess.ServiceController s inservices) { if (s.ServiceName ==serviceName) { exists = true; break; } } returnexists; }
获取服务状态的方法
/// <summary> ///获取服务状态 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public static String GetServiceStatus(stringserviceName) { string result = "服务不存在"; System.ServiceProcess.ServiceController[] services =System.ServiceProcess.ServiceController.GetServices(); foreach (System.ServiceProcess.ServiceController s inservices) { if (s.ServiceName ==serviceName) { result =s.Status.ToString(); break; } } returnresult; }
注:
服务状态返回值是枚举类型,具体返回值如下
//摘要: //指示服务的当前状态。 public enumServiceControllerStatus { //摘要: //服务未运行。这对应于 Win32 SERVICE_STOPPED 常数,该常数定义为 0x00000001。 Stopped = 1, // //摘要: //服务正在启动。这对应于 Win32 SERVICE_START_PENDING 常数,该常数定义为 0x00000002。 StartPending = 2, // //摘要: //服务正在停止。这对应于 Win32 SERVICE_STOP_PENDING 常数,该常数定义为 0x00000003。 StopPending = 3, // //摘要: //服务正在运行。这对应于 Win32 SERVICE_RUNNING 常数,该常数定义为 0x00000004。 Running = 4, // //摘要: //服务即将继续。这对应于 Win32 SERVICE_CONTINUE_PENDING 常数,该常数定义为 0x00000005。 ContinuePending = 5, // //摘要: //服务即将暂停。这对应于 Win32 SERVICE_PAUSE_PENDING 常数,该常数定义为 0x00000006。 PausePending = 6, // //摘要: //服务已暂停。这对应于 Win32 SERVICE_PAUSED 常数,该常数定义为 0x00000007。 Paused = 7, }
启动服务的方法
/// <summary> ///启动服务 /// </summary> /// <param name="serivceExeFullPath">服务全路径</param> /// <param name="serviceName">服务名</param> /// <returns></returns> public static bool ServiceStart(string serivceExeFullPath ,stringserviceName) { if (!IsServiceInstalled(serviceName)) { MessageBox.Show("服务未安装,请先安装!"); return false; } try{ using (System.Diagnostics.Process p = newSystem.Diagnostics.Process()) { p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName =serivceExeFullPath; p.StartInfo.Arguments = "start"; p.Start(); p.Close(); } System.Threading.Thread.Sleep(2000); return true; } catch(Exception ex) { MessageBox.Show("服务安装异常:" +ex.Message); return false; } }
停止服务的方法
/// <summary> ///停止服务 /// </summary> /// <param name="serivceExeFullPath">服务全路径</param> /// <param name="serviceName">服务名</param> /// <returns></returns> public static bool ServiceStop(string serivceExeFullPath, stringserviceName) { if (!IsServiceInstalled(serviceName)) { MessageBox.Show("服务未安装,请先安装!"); return false; } try{ using (System.Diagnostics.Process p = newSystem.Diagnostics.Process()) { p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName =serivceExeFullPath; p.StartInfo.Arguments = "stop"; p.Start(); p.WaitForExit(); p.Close(); } System.Threading.Thread.Sleep(2000); return true; } catch(Exception ex) { MessageBox.Show("服务停止异常:" +ex.Message); return false; } }
重启服务示例
在重启服务的按钮的点击事件中
//检测服务是否安装 bool isInstalled =WinServiceHelper.IsServiceInstalled(Global.BTS_DATA_SERVICE_NAME); if (!isInstalled) { MessageBox.Show("重启失败,服务"+Global.BTS_DATA_SERVICE_NAME+"未安装或未启动"); return; } string serviceStatus =WinServiceHelper.GetServiceStatus(Global.BTS_DATA_SERVICE_NAME); if (!serviceStatus.Equals(System.ServiceProcess.ServiceControllerStatus.Running.ToString())) { MessageBox.Show("重启失败,服务" + Global.BTS_DATA_SERVICE_NAME + "状态为:" +serviceStatus); return; } string serivceExeFullPath =Global.AppConfig.BtsDataServiceExe; string serviceName =Global.BTS_DATA_SERVICE_NAME; bool isStopSuccess =WinServiceHelper.ServiceStop(serivceExeFullPath,serviceName); //停止失败 if (!isStopSuccess) { MessageBox.Show("重启失败,服务" + Global.BTS_DATA_SERVICE_NAME + "停止失败"); return; } //方法里已经休眠2秒 bool isStartSuccess =WinServiceHelper.ServiceStart(serivceExeFullPath, serviceName); //启动失败 if (!isStartSuccess) { MessageBox.Show("重启失败,服务" + Global.BTS_DATA_SERVICE_NAME + "启动失败"); return; } MessageBox.Show("服务" + Global.BTS_DATA_SERVICE_NAME + "重启成功");