PRSInstaller.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Collections;
  2. using System.ComponentModel;
  3. using System.Configuration.Install;
  4. using System.IO;
  5. using System.Security.Principal;
  6. using System.ServiceProcess;
  7. namespace PRSServices;
  8. public class PRSInstaller : Installer
  9. {
  10. private readonly ServiceProcessInstaller _proc;
  11. private readonly ServiceInstaller _svc;
  12. public PRSInstaller()
  13. {
  14. _proc = new ServiceProcessInstaller();
  15. _svc = new ServiceInstaller { StartType = ServiceStartMode.Automatic };
  16. Installers.Add(_svc);
  17. Installers.Add(_proc);
  18. }
  19. protected override void OnBeforeInstall(IDictionary savedState)
  20. {
  21. var username = Context.Parameters["/username"];
  22. if (string.IsNullOrWhiteSpace(username))
  23. {
  24. _proc.Account = ServiceAccount.LocalSystem;
  25. _proc.Username = WindowsIdentity.GetCurrent().Name;
  26. }
  27. else
  28. {
  29. _proc.Account = ServiceAccount.User;
  30. _proc.Username = username;
  31. _proc.Password = Context.Parameters["/password"] ?? "";
  32. }
  33. _svc.ServiceName = Context.Parameters["/name"];
  34. _svc.DisplayName = Context.Parameters["/displayName"];
  35. _svc.Description = Context.Parameters["/description"];
  36. // Change to .exe because .NET creates and runs .dll
  37. Context.Parameters["assemblypath"] =
  38. "\"" + Path.ChangeExtension(Context.Parameters["assemblypath"], "exe") + "\" /service=" + _svc.ServiceName;
  39. base.OnBeforeInstall(savedState);
  40. }
  41. protected override void OnBeforeUninstall(IDictionary savedState)
  42. {
  43. _svc.ServiceName = Context.Parameters["/name"];
  44. // Change to .exe because .NET creates and runs .dll
  45. Context.Parameters["assemblypath"] =
  46. "\"" + Path.ChangeExtension(Context.Parameters["assemblypath"], "exe") + "\" /service=" + _svc.ServiceName;
  47. base.OnBeforeUninstall(savedState);
  48. }
  49. }