123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.ComponentModel;
- namespace LYFZ.WindowsServiceHandling
- {
-
-
-
- public enum ServiceType
- {
-
-
-
- [Description("LYFZ.WcfSystmeService.exe")]
- LYFZ_WcfSystmeService,
-
-
-
- [Description("Test.exe")]
- Test
-
- }
-
-
-
- public class ServiceManagement
- {
-
-
-
-
-
- public static string GetServiceProgramName(ServiceType e)
- {
- return LYFZ.WinAPI.CustomPublicMethod.ConvertEnumToDescription(e);
- }
-
-
-
-
-
- public static string GetServiceName(ServiceType e)
- {
- return e.ToString();
- }
-
-
-
-
-
- public static bool InstallService(string ServiceProgram)
- {
- string[] args = { ServiceProgram };
- return ExecuteServiceCommand(args);
- }
-
-
-
-
-
- public static bool UninstallService(string ServiceProgram)
- {
- string[] args = { "/u", ServiceProgram };
- return ExecuteServiceCommand(args);
- }
-
-
-
-
-
- public static bool ExecuteServiceCommand(string[] args)
- {
- try
- {
- System.Configuration.Install.ManagedInstallerClass.InstallHelper(args);
-
- return true;
- }
- catch
- {
- return false;
- }
- }
-
-
-
-
-
- public static bool ServiceIsExisted(string svcName)
- {
- System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();
- foreach (System.ServiceProcess.ServiceController s in services)
- {
- if (s.ServiceName == svcName)
- {
- return true;
- }
- }
- return false;
- }
-
-
-
-
-
- public static bool CheckServiceIsRunning(string ServiceName)
- {
- try
- {
- System.ServiceProcess.ServiceController s = new System.ServiceProcess.ServiceController(ServiceName);
- if (s.Status == System.ServiceProcess.ServiceControllerStatus.Running)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch { return false; }
- }
-
-
-
-
- public static string StartService(string ServiceName)
- {
- if (ServiceIsExisted(ServiceName))
- {
- System.ServiceProcess.ServiceController s = new System.ServiceProcess.ServiceController(ServiceName);
- if (s.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
- {
- s.Start();
- return("“"+s.DisplayName+"”启动成功");
-
- }
- else
- {
- return ("“" + s.DisplayName + "”正在运行...");
- }
- }
- else {
- return ("要启动的服务不存在,请先安装服务...");
- }
- }
-
-
-
-
- public static string StopService(string ServiceName)
- {
- if (ServiceIsExisted(ServiceName))
- {
- System.ServiceProcess.ServiceController s = new System.ServiceProcess.ServiceController(ServiceName);
-
- if (s.Status == System.ServiceProcess.ServiceControllerStatus.Running)
- {
- s.Stop();
- return ("“" + s.DisplayName + "”停止成功");
- }
- else
- {
- return ("“" + s.DisplayName + "”已经停止");
- }
- }
- else
- {
- return ("要停止的服务不存在,无需停止!");
- }
- }
- #region 服务其它管理
- #endregion
- }
- }
|