PRSInstaller.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 PRSServer
  8. {
  9. [RunInstaller(true)]
  10. public class PRSInstaller : Installer
  11. {
  12. private readonly ServiceProcessInstaller _proc;
  13. private readonly ServiceInstaller _svc;
  14. public PRSInstaller()
  15. {
  16. _proc = new ServiceProcessInstaller();
  17. _svc = new ServiceInstaller { StartType = ServiceStartMode.Automatic };
  18. Installers.Add(_svc);
  19. Installers.Add(_proc);
  20. }
  21. protected override void OnBeforeInstall(IDictionary savedState)
  22. {
  23. var username = Context.Parameters["/username"];
  24. if (string.IsNullOrWhiteSpace(username))
  25. {
  26. _proc.Account = ServiceAccount.LocalSystem;
  27. _proc.Username = WindowsIdentity.GetCurrent().Name;
  28. }
  29. else
  30. {
  31. _proc.Account = ServiceAccount.User;
  32. _proc.Username = username;
  33. _proc.Password = Context.Parameters["/password"] ?? "";
  34. }
  35. _svc.ServiceName = Context.Parameters["/name"];
  36. _svc.DisplayName = Context.Parameters["/displayName"];
  37. _svc.Description = Context.Parameters["/description"];
  38. // Change to .exe because .NET creates and runs .dll
  39. Context.Parameters["assemblypath"] =
  40. "\"" + Path.ChangeExtension(Context.Parameters["assemblypath"], "exe") + "\" /service=" + _svc.ServiceName;
  41. base.OnBeforeInstall(savedState);
  42. }
  43. protected override void OnBeforeUninstall(IDictionary savedState)
  44. {
  45. _svc.ServiceName = Context.Parameters["/name"];
  46. // Change to .exe because .NET creates and runs .dll
  47. Context.Parameters["assemblypath"] =
  48. "\"" + Path.ChangeExtension(Context.Parameters["assemblypath"], "exe") + "\" /service=" + _svc.ServiceName;
  49. base.OnBeforeUninstall(savedState);
  50. }
  51. }
  52. }