C#操作windows服务,安装、卸载、停止、启动

  1    public class ServiceUtil
  2     {
  3         private string _ServiceName = string.Empty;
  4         private string _AppName = string.Empty;
  5 
  6         public string AppName
  7         {
  8             get { return _AppName; }
  9             set { _AppName = value; }
 10         }
 11 
 12         public string ServiceName
 13         {
 14             get { return _ServiceName; }
 15         }
 16 
 17         /// <summary>
 18         /// 
 19         /// </summary>
 20         /// <param name="appName">后缀名为.exe的文件</param>
 21         /// <param name="serviceName"></param>
 22         public ServiceUtil(string appName, string serviceName)
 23         {
 24             _AppName = appName;
 25             _ServiceName = serviceName;
 26         }
 27 
 28         #region 启动服务
 29         /// <summary>
 30         /// StartService
 31         /// </summary>
 32         public void StartService()
 33         {
 34             ServiceController sc = new ServiceController(_ServiceName);
 35             if (sc.Status.Equals(ServiceControllerStatus.Stopped))
 36             {
 37                 sc.Start();
 38             }
 39         }
 40         #endregion
 41 
 42         #region 停止服务
 43         /// <summary>
 44         /// 停止服务
 45         /// </summary>
 46         public void StopService()
 47         {
 48             ServiceController sc = new ServiceController(_ServiceName);
 49             if (!sc.Status.Equals(ServiceControllerStatus.Stopped))
 50             {
 51                 sc.Stop();
 52             }
 53         }
 54 
 55         #endregion
 56 
 57         #region 安装服务
 58         /// <summary>
 59         /// 安装服务
 60         /// </summary>
 61         public void InstallService()
 62         {
 63             if (!isServiceIsExisted(_ServiceName))
 64             {
 65                 string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
 66                 string serviceFileName = location.Substring(0, location.LastIndexOf('\') + 1) + string.Format("{0}.exe", _AppName);
 67                 InstallmyService(null, serviceFileName);
 68             }
 69         }
 70         #endregion
 71 
 72         #region 卸载服务
 73         public void UnInstallService()
 74         {
 75             if (isServiceIsExisted(_ServiceName))
 76             {
 77                 string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
 78                 string serviceFileName = location.Substring(0, location.LastIndexOf('\') + 1) + string.Format("{0}.exe", _AppName);
 79                 UnInstallmyService(serviceFileName);
 80             }
 81         }
 82         #endregion
 83 
 84         #region 检查服务存在的存在性
 85         /// <summary>
 86         /// 检查服务存在的存在性
 87         /// </summary>
 88         /// <param name=" NameService ">服务名</param>
 89         /// <returns>存在返回 true,否则返回 false;</returns>
 90         public static bool isServiceIsExisted(string NameService)
 91         {
 92             ServiceController[] services = ServiceController.GetServices();
 93             foreach (ServiceController s in services)
 94             {
 95                 if (s.ServiceName.ToLower() == NameService.ToLower())
 96                 {
 97                     return true;
 98                 }
 99             }
100             return false;
101         }
102         #endregion
103 
104         #region Private
105         #region 安装Windows服务
106         /// <summary>
107         /// 安装Windows服务
108         /// </summary>
109         /// <param name="stateSaver">集合</param>
110         /// <param name="filepath">程序文件路径</param>
111         private void InstallmyService(IDictionary stateSaver, string filepath)
112         {
113 
114             AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
115 
116             AssemblyInstaller1.UseNewContext = true;
117 
118             AssemblyInstaller1.Path = filepath;
119 
120             AssemblyInstaller1.Install(stateSaver);
121 
122             AssemblyInstaller1.Commit(stateSaver);
123 
124             AssemblyInstaller1.Dispose();
125 
126         }
127         #endregion
128 
129         #region 卸载Windows服务
130         /// <summary>
131         /// 卸载Windows服务
132         /// </summary>
133         /// <param name="filepath">程序文件路径</param>
134         private void UnInstallmyService(string filepath)
135         {
136             AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
137 
138             AssemblyInstaller1.UseNewContext = true;
139 
140             AssemblyInstaller1.Path = filepath;
141 
142             AssemblyInstaller1.Uninstall(null);
143 
144             AssemblyInstaller1.Dispose();
145 
146         }
147         #endregion
148         #endregion
149 
150     }

 测试环境:win8+vs2012+.net4.0

原文地址:https://www.cnblogs.com/wz122889488/p/4915136.html