首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

原创 C#操作登记服务卸载服务启动服务停止服务

2012-09-14 
原创C#操作注册服务卸载服务启动服务停止服务using?System??using?System.Configuration.Install??using

原创 C#操作注册服务卸载服务启动服务停止服务

  1. using?System;??using?System.Configuration.Install;??
  2. using?System.Collections;??using?System.Collections.Specialized;??
  3. ??IDictionary?stateSaver?=?new?Hashtable();??
  4. 一、安装服务:??private?void?InstallService(IDictionary?stateSaver,?string?filepath)??
  5. ??????????{??
  6. ??????????????try??
  7. ??????????????{??
  8. ??????????????????System.ServiceProcess.ServiceController?service?=?new?System.ServiceProcess.ServiceController("ServiceName");??
  9. ??????????????????if(!ServiceIsExisted("ServiceName"))??
  10. ??????????????????{??
  11. ??????????????????????//Install?Service??
  12. ??????????????????????AssemblyInstaller?myAssemblyInstaller?=?new?AssemblyInstaller();??
  13. ??????????????????????myAssemblyInstaller.UseNewContext?=?true;??
  14. ??????????????????????myAssemblyInstaller.Path?=filepath;??
  15. ??????????????????????myAssemblyInstaller.Install(stateSaver);??
  16. ??????????????????????myAssemblyInstaller.Commit(stateSaver);??
  17. ??????????????????????myAssemblyInstaller.Dispose();??
  18. ??????????????????????//--Start?Service??
  19. ??????????????????????service.Start();??
  20. ??????????????????}??
  21. ??????????????????else??
  22. ??????????????????{??
  23. ??????????????????????if?(service.Status?!=?System.ServiceProcess.ServiceControllerStatus.Running?&&?service.Status?!=?System.ServiceProcess.ServiceControllerStatus.StartPending)??
  24. ??????????????????????{??
  25. ??????????????????????????service.Start();??
  26. ??????????????????????}??
  27. ??????????????????}??
  28. ??????????????}??
  29. ??????????????catch?(Exception?ex)??
  30. ??????????????{??
  31. ??????????????????throw?new?Exception("installServiceError\n"?+?ex.Message);??
  32. ??????????????}??
  33. ??????????}??
  34. ??或者??
  35. ?????????///?<summary>??????????///?安装服务??
  36. ????????///?</summary>??????????///?<param?name="p_Path">指定服务文件路径</param>??
  37. ????????///?<param?name="p_ServiceName">返回安装完成后的服务名</param>??????????///?<returns>安装信息?正确安装返回""</returns>??
  38. ????????public?static?string?InsertService(string?p_Path,?ref?string?p_ServiceName)??????????{??
  39. ????????????if?(!File.Exists(p_Path))?return?"文件不存在!";??????????????p_ServiceName?=?"";??
  40. ????????????FileInfo?_InsertFile?=?new?FileInfo(p_Path);??????????????IDictionary?_SavedState?=?new?Hashtable();??
  41. ????????????try??????????????{??
  42. ????????????????//加载一个程序集,并运行其中的所有安装程序。??????????????????AssemblyInstaller?_AssemblyInstaller?=?new?AssemblyInstaller(p_Path,?new?string[]?{?"/LogFile="?+?_InsertFile.DirectoryName?+?"\"?+?_InsertFile.Name.Substring(0,?_InsertFile.Name.Length?-?_InsertFile.Extension.Length)?+?".log"?});??
  43. ????????????????_AssemblyInstaller.UseNewContext?=?true;??????????????????_AssemblyInstaller.Install(_SavedState);??
  44. ????????????????_AssemblyInstaller.Commit(_SavedState);??????????????????Type[]?_TypeList?=?_AssemblyInstaller.Assembly.GetTypes();//获取安装程序集类型集合??
  45. ????????????????for?(int?i?=?0;?i?!=?_TypeList.Length;?i++)??????????????????{??
  46. ????????????????????if?(_TypeList[i].BaseType.FullName?==?"System.Configuration.Install.Installer")??????????????????????{??
  47. ????????????????????????//找到System.Configuration.Install.Installer?类型??????????????????????????object?_InsertObject?=?System.Activator.CreateInstance(_TypeList[i]);//创建类型实列??
  48. ????????????????????????FieldInfo[]?_FieldList?=?_TypeList[i].GetFields(BindingFlags.NonPublic?|?BindingFlags.Instance);??????????????????????????for?(int?z?=?0;?z?!=?_FieldList.Length;?z++)??
  49. ????????????????????????{??????????????????????????????if?(_FieldList[z].FieldType.FullName?==?"System.ServiceProcess.ServiceInstaller")??
  50. ????????????????????????????{??????????????????????????????????object?_ServiceInsert?=?_FieldList[z].GetValue(_InsertObject);??
  51. ????????????????????????????????if?(_ServiceInsert?!=?null)??????????????????????????????????{??
  52. ????????????????????????????????????p_ServiceName?=?((ServiceInstaller)_ServiceInsert).ServiceName;??????????????????????????????????????return?"";??
  53. ????????????????????????????????}??????????????????????????????}??
  54. ????????????????????????}??????????????????????}??
  55. ????????????????}??????????????????return?"";??
  56. ????????????}??????????????catch?(Exception?ex)??
  57. ????????????{??????????????????return?ex.Message;??
  58. ????????????}??????????}??
  59. ??二、卸载windows服务:??
  60. ??????????private?void?UnInstallService(string?filepath)??
  61. ??????????{??
  62. ??????????????try??
  63. ??????????????{??
  64. ??????????????????if?(ServiceIsExisted("ServiceName"))??
  65. ??????????????????{??
  66. ??????????????????????//UnInstall?Service??
  67. ??????????????????????AssemblyInstaller?myAssemblyInstaller?=?new?AssemblyInstaller();??
  68. ??????????????????????myAssemblyInstaller.UseNewContext?=?true;??
  69. ??????????????????????myAssemblyInstaller.Path?=?filepath;??
  70. ??????????????????????myAssemblyInstaller.Uninstall(null);??
  71. ??????????????????????myAssemblyInstaller.Dispose();??
  72. ??????????????????}??
  73. ??????????????}??
  74. ??????????????catch?(Exception?ex)??
  75. ??????????????{??
  76. ??????????????????throw?new?Exception("unInstallServiceError\n"?+?ex.Message);??
  77. ??????????????}??
  78. ??????????}??
  79. ??三、判断window服务是否存在:??
  80. ??????????private?bool?ServiceIsExisted(string?serviceName)??
  81. ??????????{??
  82. ??????????????ServiceController[]?services?=?ServiceController.GetServices();??
  83. ??????????????foreach?(ServiceController?s?in?services)??
  84. ??????????????{??
  85. ??????????????????if?(s.ServiceName?==?serviceName)??
  86. ??????????????????{??
  87. ??????????????????????return?true;??
  88. ??????????????????}??
  89. ??????????????}??
  90. ??????????????return?false;??
  91. ??????????}??
  92. ??四、启动服务:??
  93. ??private?void?StartService(string?serviceName)??
  94. ??????????{??
  95. ??????????????if?(ServiceIsExisted(serviceName))??
  96. ??????????????{??
  97. ??????????????????System.ServiceProcess.ServiceController?service?=?new?System.ServiceProcess.ServiceController(serviceName);??
  98. ??????????????????if?(service.Status?!=?System.ServiceProcess.ServiceControllerStatus.Running?&&?service.Status?!=?System.ServiceProcess.ServiceControllerStatus.StartPending)??
  99. ??????????????????{??
  100. ??????????????????????service.Start();??
  101. ??????????????????????for?(int?i?=?0;?i?<?60;?i++)??
  102. ??????????????????????{??
  103. ??????????????????????????service.Refresh();??
  104. ??????????????????????????System.Threading.Thread.Sleep(1000);??
  105. ??????????????????????????if?(service.Status?==?System.ServiceProcess.ServiceControllerStatus.Running)??
  106. ??????????????????????????{??
  107. ??????????????????????????????break;??
  108. ??????????????????????????}??
  109. ??????????????????????????if?(i?==?59)??
  110. ??????????????????????????{??
  111. ??????????????????????????????throw?new?Exception(startServiceError.Replace("$s$",?serviceName));??
  112. ??????????????????????????}??
  113. ??????????????????????}??
  114. ??????????????????}??
  115. ??????????????}??
  116. ??????????}??
  117. 另外方法??????????///?<summary>??
  118. ????????///?启动服务??????????///?</summary>??
  119. ????????///?<param?name="serviceName"></param>??????????///?<returns></returns>??
  120. ????????public?static?bool?ServiceStart(string?serviceName)??????????{??
  121. ????????????try??????????????{??
  122. ????????????????ServiceController?service?=?new?ServiceController(serviceName);??????????????????if?(service.Status?==?ServiceControllerStatus.Running)??
  123. ????????????????{??????????????????????return?true;??
  124. ????????????????}??????????????????else??
  125. ????????????????{??????????????????????TimeSpan?timeout?=?TimeSpan.FromMilliseconds(1000?*?10);??
  126. ??????????????????????service.Start();??
  127. ????
  128. ????????????????????service.WaitForStatus(ServiceControllerStatus.Running,?timeout);??????????????????}??
  129. ????????????}??????????????catch??
  130. ????????????{????
  131. ????????????????return?false;??????????????}??
  132. ????????????return?true;????
  133. ????????}????
  134. 五、停止服务:????
  135. ????????private?void?StopService(string?serviceName)????
  136. ????????{????
  137. ????????????if?(ServiceIsExisted(serviceName))????
  138. ????????????{????
  139. ????????????????System.ServiceProcess.ServiceController?service?=?new?System.ServiceProcess.ServiceController(serviceName);????
  140. ????????????????if?(service.Status?==?System.ServiceProcess.ServiceControllerStatus.Running)????
  141. ????????????????{????
  142. ????????????????????service.Stop();????
  143. ????????????????????for?(int?i?=?0;?i?<?60;?i++)????
  144. ????????????????????{????
  145. ????????????????????????service.Refresh();????
  146. ????????????????????????System.Threading.Thread.Sleep(1000);????
  147. ????????????????????????if?(service.Status?==?System.ServiceProcess.ServiceControllerStatus.Stopped)????
  148. ????????????????????????{????
  149. ????????????????????????????break;????
  150. ????????????????????????}????
  151. ????????????????????????if?(i?==?59)????
  152. ????????????????????????{????
  153. ????????????????????????????throw?new?Exception(stopServiceError.Replace("$s$",?serviceName));????
  154. ????????????????????????}????
  155. ????????????????????}????
  156. ????????????????}????
  157. ????????????}????
  158. ????????}??另外一方法:??
  159. ?????????///?<summary>??????????///?停止服务??
  160. ????????///?</summary>??????????///?<param?name="serviseName"></param>??
  161. ????????///?<returns></returns>??????????public?static?bool?ServiceStop(string?serviseName)??
  162. ????????{??????????????try??
  163. ????????????{??????????????????ServiceController?service?=?new?ServiceController(serviseName);??
  164. ????????????????if?(service.Status?==?ServiceControllerStatus.Stopped)??????????????????{??
  165. ????????????????????return?true;??????????????????}??
  166. ????????????????else??????????????????{??
  167. ????????????????????TimeSpan?timeout?=?TimeSpan.FromMilliseconds(1000?*?10);??????????????????????service.Stop();??
  168. ????????????????????service.WaitForStatus(ServiceControllerStatus.Running,?timeout);??????????????????}??
  169. ????????????}??????????????catch??
  170. ????????????{????
  171. ????????????????return?false;??????????????}??
  172. ????????????return?true;??????????}??
  173. 六?修改服务的启动项???????????///?<summary>??
  174. ????????///?修改服务的启动项?2为自动,3为手动??????????///?</summary>??
  175. ????????///?<param?name="startType"></param>??????????///?<param?name="serviceName"></param>??
  176. ????????///?<returns></returns>??????????public?static?bool?ChangeServiceStartType(int?startType,?string?serviceName)??
  177. ????????{??????????????try??
  178. ????????????{??????????????????RegistryKey?regist?=?Registry.LocalMachine;??
  179. ????????????????RegistryKey?sysReg?=?regist.OpenSubKey("SYSTEM");??????????????????RegistryKey?currentControlSet?=?sysReg.OpenSubKey("CurrentControlSet");??
  180. ????????????????RegistryKey?services?=?currentControlSet.OpenSubKey("Services");??????????????????RegistryKey?servicesName?=?services.OpenSubKey(serviceName,?true);??
  181. ????????????????servicesName.SetValue("Start",?startType);??????????????}??
  182. ????????????catch?(Exception?ex)??????????????{??
  183. ??????????????????return?false;??
  184. ????????????}??????????????return?true;??
  185. ????
  186. ????????}????
  187. 七?获取服务启动类型???????????///?<summary>??
  188. ????????///?获取服务启动类型?2为自动?3为手动?4?为禁用??????????///?</summary>??
  189. ????????///?<param?name="serviceName"></param>??????????///?<returns></returns>??
  190. ????????public?static?string?GetServiceStartType(string?serviceName)??????????{??
  191. ??????????????try??
  192. ????????????{??????????????????RegistryKey?regist?=?Registry.LocalMachine;??
  193. ????????????????RegistryKey?sysReg?=?regist.OpenSubKey("SYSTEM");??????????????????RegistryKey?currentControlSet?=?sysReg.OpenSubKey("CurrentControlSet");??
  194. ????????????????RegistryKey?services?=?currentControlSet.OpenSubKey("Services");??????????????????RegistryKey?servicesName?=?services.OpenSubKey(serviceName,?true);??
  195. ????????????????return?servicesName.GetValue("Start").ToString();??????????????}??
  196. ????????????catch?(Exception?ex)??????????????{??
  197. ??????????????????return?string.Empty;??
  198. ????????????}????
  199. ????????}????
  200. 八?验证服务是否启动??服务是否存在???????????///?<summary>??
  201. ????????///?验证服务是否启动??????????///?</summary>??
  202. ????????///?<returns></returns>??????????public?static?bool?ServiceIsRunning(string?serviceName)??
  203. ????????{??????????????ServiceController?service?=?new?ServiceController(serviceName);??
  204. ????????????if?(service.Status?==?ServiceControllerStatus.Running)??????????????{??
  205. ????????????????return?true;??????????????}??
  206. ????????????else??????????????{??
  207. ????????????????return?false;??????????????}??
  208. ????????}????
  209. ????????///?<summary>??????????///?服务是否存在??
  210. ????????///?</summary>??????????///?<param?name="serviceName"></param>??
  211. ????????///?<returns></returns>??????????public?static?bool?ServiceExist(string?serviceName)??
  212. ????????{??????????????try??
  213. ????????????{??????????????????string?m_ServiceName?=?serviceName;??
  214. ????????????????ServiceController[]?services?=?ServiceController.GetServices();??????????????????foreach?(ServiceController?s?in?services)??
  215. ????????????????{??????????????????????if?(s.ServiceName.ToLower()?==?m_ServiceName.ToLower())??
  216. ????????????????????{??????????????????????????return?true;??
  217. ????????????????????}??????????????????}??
  218. ????????????????return?false;??????????????}??
  219. ????????????catch?(Exception?ex)??????????????{??
  220. ????????????????return?false;??????????????}??
  221. ????????}??注:手动安装window服务的方法:??
  222. ??在“Visual?Studio?2005?命令提示”窗口中,运行:??
  223. ??安装服务:installutil?servicepath??
  224. ??卸除服务:installutil?/u?servicepath?

热点排行