日期:2014-05-18  浏览次数:20738 次

原创 C#操作注册服务卸载服务启动服务停止服务
  1. using ?System;??
  2. using ?System.Configuration.Install;??
  3. using ?System.Collections;??
  4. using ?System.Collections.Specialized;??
  5. ??
  6. IDictionary?stateSaver?=?new ?Hashtable();??
  7. 一、安装服务:??
  8. private ? void ?InstallService(IDictionary?stateSaver,? string ?filepath)??
  9. ??
  10. ????????{??
  11. ??
  12. ????????????try ??
  13. ??
  14. ????????????{??
  15. ??
  16. ????????????????System.ServiceProcess.ServiceController?service?=?new ?System.ServiceProcess.ServiceController( "ServiceName" );??
  17. ??
  18. ????????????????if (!ServiceIsExisted( "ServiceName" ))??
  19. ??
  20. ????????????????{??
  21. ??
  22. ????????????????????//Install?Service ??
  23. ??
  24. ????????????????????AssemblyInstaller?myAssemblyInstaller?=?new ?AssemblyInstaller();??
  25. ??
  26. ????????????????????myAssemblyInstaller.UseNewContext?=?true ;??
  27. ??
  28. ????????????????????myAssemblyInstaller.Path?=filepath;??
  29. ??
  30. ????????????????????myAssemblyInstaller.Install(stateSaver);??
  31. ??
  32. ????????????????????myAssemblyInstaller.Commit(stateSaver);??
  33. ??
  34. ????????????????????myAssemblyInstaller.Dispose();??
  35. ??
  36. ????????????????????//--Start?Service ??
  37. ??
  38. ????????????????????service.Start();??
  39. ??
  40. ????????????????}??
  41. ??
  42. ????????????????else ??
  43. ??
  44. ????????????????{??
  45. ??
  46. ????????????????????if ?(service.Status?!=?System.ServiceProcess.ServiceControllerStatus.Running?&&?service.Status?!=?System.ServiceProcess.ServiceControllerStatus.StartPending)??
  47. ??
  48. ????????????????????{??
  49. ??
  50. ????????????????????????service.Start();??
  51. ??
  52. ????????????????????}??
  53. ??
  54. ????????????????}??
  55. ??
  56. ?????????