code_junkie

Install a .NET windows service without InstallUtil.exe

Nov 12th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. string[] args;
  2. ManagedInstallerClass.InstallHelper(args);
  3.  
  4. IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
  5. Inst.Install("MySvc", "My Sample Service", "Service that executes something",
  6. _InstanceID,
  7. // System.ServiceProcess.ServiceAccount.LocalService, // this is more secure, but only available in XP and above and WS-2003 and above
  8. System.ServiceProcess.ServiceAccount.LocalSystem, // this is required for WS-2000
  9. System.ServiceProcess.ServiceStartMode.Automatic);
  10. if (controller == null)
  11. {
  12. controller = new System.ServiceProcess.ServiceController(String.Format("MySvc_{0}", _InstanceID), ".");
  13. }
  14. if (controller.Status == System.ServiceProcess.ServiceControllerStatus.Running)
  15. {
  16. Start_Stop.Text = "Stop Service";
  17. Start_Stop_Debugging.Enabled = false;
  18. }
  19. else
  20. {
  21. Start_Stop.Text = "Start Service";
  22. Start_Stop_Debugging.Enabled = true;
  23. }
  24.  
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Text;
  28. using System.Diagnostics;
  29. using Microsoft.Win32;
  30.  
  31. namespace MySvc
  32. {
  33. class IntegratedServiceInstaller
  34. {
  35. public void Install(String ServiceName, String DisplayName, String Description,
  36. String InstanceID,
  37. System.ServiceProcess.ServiceAccount Account,
  38. System.ServiceProcess.ServiceStartMode StartMode)
  39. {
  40. //http://www.theblacksparrow.com/
  41. System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
  42. ProcessInstaller.Account = Account;
  43.  
  44. System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();
  45.  
  46. System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
  47. string processPath = Process.GetCurrentProcess().MainModule.FileName;
  48. if (processPath != null && processPath.Length > 0)
  49. {
  50. System.IO.FileInfo fi = new System.IO.FileInfo(processPath);
  51.  
  52. String path = String.Format("/assemblypath={0}", fi.FullName);
  53. String[] cmdline = { path };
  54. Context = new System.Configuration.Install.InstallContext("", cmdline);
  55. }
  56.  
  57. SINST.Context = Context;
  58. SINST.DisplayName = String.Format("{0} - {1}", DisplayName, InstanceID);
  59. SINST.Description = String.Format("{0} - {1}", Description, InstanceID);
  60. SINST.ServiceName = String.Format("{0}_{1}", ServiceName, InstanceID);
  61. SINST.StartType = StartMode;
  62. SINST.Parent = ProcessInstaller;
  63.  
  64. // http://bytes.com/forum/thread527221.html
  65. SINST.ServicesDependedOn = new String[] { "Spooler", "Netlogon", "Netman" };
  66.  
  67. System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
  68. SINST.Install(state);
  69.  
  70. // http://www.dotnet247.com/247reference/msgs/43/219565.aspx
  71. using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(@"SYSTEMCurrentControlSetServices{0}_{1}", ServiceName, InstanceID), true))
  72. {
  73. try
  74. {
  75. Object sValue = oKey.GetValue("ImagePath");
  76. oKey.SetValue("ImagePath", sValue);
  77. }
  78. catch (Exception Ex)
  79. {
  80. System.Windows.Forms.MessageBox.Show(Ex.Message);
  81. }
  82. }
  83.  
  84. }
  85. public void Uninstall(String ServiceName, String InstanceID)
  86. {
  87. //http://www.theblacksparrow.com/
  88. System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();
  89.  
  90. System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext("c:\install.log", null);
  91. SINST.Context = Context;
  92. SINST.ServiceName = String.Format("{0}_{1}", ServiceName, InstanceID);
  93. SINST.Uninstall(null);
  94. }
  95. }
  96. }
Add Comment
Please, Sign In to add comment