ProjectInstaller.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Technitium Library
  3. Copyright (C) 2018 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System.Collections;
  16. using System.ComponentModel;
  17. using System.Configuration.Install;
  18. using System.ServiceProcess;
  19. namespace DnsService
  20. {
  21. [RunInstaller(true)]
  22. public partial class ProjectInstaller : Installer
  23. {
  24. public ProjectInstaller()
  25. {
  26. InitializeComponent();
  27. }
  28. protected override void OnBeforeInstall(IDictionary savedState)
  29. {
  30. try
  31. {
  32. foreach (ServiceController sc in ServiceController.GetServices())
  33. {
  34. if (sc.ServiceName == serviceInstaller1.ServiceName)
  35. {
  36. //found previously installed service
  37. //stop service
  38. if (sc.Status == ServiceControllerStatus.Running)
  39. sc.Stop();
  40. //uninstall service
  41. using (ServiceInstaller si = new ServiceInstaller())
  42. {
  43. si.Context = new InstallContext();
  44. si.ServiceName = serviceInstaller1.ServiceName;
  45. si.Uninstall(null);
  46. }
  47. break;
  48. }
  49. }
  50. }
  51. catch
  52. { }
  53. }
  54. protected override void OnAfterInstall(IDictionary savedState)
  55. {
  56. try
  57. {
  58. using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
  59. {
  60. sc.Start();
  61. }
  62. }
  63. catch
  64. { }
  65. }
  66. protected override void OnBeforeUninstall(IDictionary savedState)
  67. {
  68. try
  69. {
  70. using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
  71. {
  72. sc.Stop();
  73. }
  74. }
  75. catch
  76. { }
  77. }
  78. }
  79. }