Guest User

Untitled

a guest
Jun 21st, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. namespace MyService
  2. {
  3. public class ServiceMonitor : ServiceBase
  4. {
  5. private System.ComponentModel.Container _components = null;
  6. private static string _service_name = "MyServiceName";
  7.  
  8. public ServiceMonitor()
  9. {
  10. InitializeComponent();
  11. }
  12.  
  13. private void InitializeComponent()
  14. {
  15. this.CanHandlePowerEvent = true;
  16. this.CanPauseAndContinue = true;
  17. this.CanShutdown = true;
  18. this.CanStop = true;
  19. this.ServiceName = _service_name;
  20. }
  21.  
  22. protected override void Dispose(bool disposing)
  23. {
  24. if (disposing && _components != null)
  25. {
  26. _components.Dispose();
  27. }
  28. base.Dispose(disposing);
  29. }
  30.  
  31. static void Main(string[] args)
  32. {
  33. string opt = null;
  34.  
  35. if (args.Length >= 1)
  36. {
  37. opt = args[0].ToLower();
  38. }
  39.  
  40.  
  41. if (opt == "/install" || opt == "/uninstall")
  42. {
  43. TransactedInstaller ti = new TransactedInstaller();
  44. MonitorInstaller mi = new MonitorInstaller(_service_name);
  45.  
  46. ti.Installers.Add(mi);
  47.  
  48. string path = String.Format("/assemblypath={0}", Assembly.GetExecutingAssembly().Location);
  49. string[] cmdline = { path };
  50.  
  51. InstallContext ctx = new InstallContext("", cmdline);
  52.  
  53. ti.Context = ctx;
  54.  
  55. if (opt == "/install")
  56. {
  57. Console.WriteLine("Installing");
  58. ti.Install(new Hashtable());
  59. }
  60. else if (opt == "/uninstall")
  61. {
  62. Console.WriteLine("Uninstalling");
  63. try
  64. {
  65. ti.Uninstall(null);
  66. }
  67. catch (InstallException ie)
  68. {
  69. Console.WriteLine(ie.ToString());
  70. }
  71. }
  72. }
  73. else
  74. {
  75. ServiceBase[] services;
  76. services = new ServiceBase[] { new ServiceMonitor() };
  77. ServiceBase.Run(services);
  78. }
  79. }
  80.  
  81. protected override void OnStart(string[] args)
  82. {
  83. //
  84. // TODO: spawn a new thread or timer to perform actions in the background.
  85. //
  86. base.OnStart(args);
  87. }
  88.  
  89. protected override void OnStop()
  90. {
  91. //
  92. // TODO: stop your thread or timer
  93. //
  94. base.OnStop();
  95. }
  96. }
  97.  
  98. [RunInstaller(true)]
  99. public class MonitorInstaller : Installer
  100. {
  101. public MonitorInstaller()
  102. : this("MyServiceName")
  103. {
  104. }
  105.  
  106. public MonitorInstaller(string service_name)
  107. {
  108. ServiceProcessInstaller spi = new ServiceProcessInstaller();
  109.  
  110. spi.Account = ServiceAccount.User;
  111. spi.Password = ConfigurationManager.AppSettings["Password"];
  112. spi.Username = ConfigurationManager.AppSettings["Username"];
  113.  
  114. ServiceInstaller si = new ServiceInstaller();
  115.  
  116. si.ServiceName = service_name;
  117. si.StartType = ServiceStartMode.Automatic;
  118. si.Description = "MyServiceName";
  119. si.DisplayName = "MyServiceName";
  120.  
  121. this.Installers.Add(spi);
  122. this.Installers.Add(si);
  123. }
  124. }
  125. }
Add Comment
Please, Sign In to add comment