Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. protected override void OnBeforeInstall(IDictionary savedState)
  2. {
  3. base.OnBeforeInstall(savedState);
  4.  
  5. bool isUserAccount = false;
  6.  
  7. string name = GetContextoParametros("name");
  8. if (0 == name.Length) name = serviceInstaller.ServiceName;
  9. serviceInstaller.ServiceName = name;
  10.  
  11. string display = GetContextoParametros("displayname");
  12. if (0 == display.Length) display = serviceInstaller.DisplayName;
  13. serviceInstaller.DisplayName = display;
  14.  
  15. string acct = GetContextoParametros("account");
  16.  
  17. switch (acct)
  18. {
  19. case "user":
  20. serviceProcessInstaller.Account =
  21. System.ServiceProcess.ServiceAccount.User;
  22. isUserAccount = true;
  23. break;
  24. case "localservice":
  25. serviceProcessInstaller.Account =
  26. System.ServiceProcess.ServiceAccount.LocalService;
  27. break;
  28. case "localsystem":
  29. serviceProcessInstaller.Account =
  30. System.ServiceProcess.ServiceAccount.LocalSystem;
  31. break;
  32. case "networkservice":
  33. serviceProcessInstaller.Account =
  34. System.ServiceProcess.ServiceAccount.NetworkService;
  35. break;
  36. }
  37.  
  38. string username = GetContextoParametros("user");
  39. string password = GetContextoParametros("password");
  40.  
  41. if (isUserAccount)
  42. {
  43. serviceProcessInstaller.Username = username;
  44. serviceProcessInstaller.Password = password;
  45. }
  46. }
  47.  
  48. public override void Install(IDictionary stateServer)
  49. {
  50. RegistryKey system,
  51. //HKEY_LOCAL_MACHINEServicesCurrentControlSet
  52. currentControlSet,
  53. //...Services
  54. services,
  55. //...<Service Name>
  56. service,
  57. //...Parameters - this is where you can
  58. //put service-specific configuration
  59. config;
  60.  
  61. base.Install(stateServer);
  62.  
  63. system = Registry.LocalMachine.OpenSubKey("System");
  64. currentControlSet = system.OpenSubKey("CurrentControlSet");
  65. services = currentControlSet.OpenSubKey("Services");
  66.  
  67. service =
  68. services.OpenSubKey(this.serviceInstaller.ServiceName, true);
  69.  
  70. service.SetValue("Description",
  71. this.serviceInstaller.Description);
  72.  
  73. Console.WriteLine("ImagePath: " + service.GetValue("ImagePath"));
  74. string imagePath = (string)service.GetValue("ImagePath");
  75. imagePath += " -s" + this.serviceInstaller.ServiceName;
  76. service.SetValue("ImagePath", imagePath);
  77. config = service.CreateSubKey("Parameters");
  78.  
  79. config.Close();
  80. service.Close();
  81. services.Close();
  82. currentControlSet.Close();
  83. system.Close();
  84. }
  85. public string GetContextoParametros(string key)
  86. {
  87. string sValue = "";
  88. try
  89. {
  90. sValue = this.Context.Parameters[key].ToString();
  91. if (sValue.IndexOf(@"") >= 0)
  92. sValue = sValue.Substring(sValue.LastIndexOf(@""), sValue.Length - sValue.LastIndexOf(@""));
  93. }
  94. catch
  95. {
  96. sValue = "";
  97. }
  98. return sValue;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement