diff --git a/DnsService/ProjectInstaller.cs b/DnsService/ProjectInstaller.cs index d81bc790..fa6da0c8 100644 --- a/DnsService/ProjectInstaller.cs +++ b/DnsService/ProjectInstaller.cs @@ -1,4 +1,24 @@ -using System.ComponentModel; +/* +Technitium Library +Copyright (C) 2018 Shreyas Zare (shreyas@technitium.com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +*/ + +using System.Collections; +using System.ComponentModel; using System.Configuration.Install; using System.ServiceProcess; @@ -10,26 +30,58 @@ namespace DnsService public ProjectInstaller() { InitializeComponent(); - - serviceInstaller1.AfterInstall += ServiceInstaller1_AfterInstall; - serviceInstaller1.BeforeUninstall += ServiceInstaller1_BeforeUninstall; } - private void ServiceInstaller1_AfterInstall(object sender, InstallEventArgs e) + protected override void OnBeforeInstall(IDictionary savedState) { try { - new ServiceController(serviceInstaller1.ServiceName).Start(); + foreach (ServiceController sc in ServiceController.GetServices()) + { + if (sc.ServiceName == serviceInstaller1.ServiceName) + { + //found previously installed service + //stop service + if (sc.Status == ServiceControllerStatus.Running) + sc.Stop(); + + //uninstall service + using (ServiceInstaller si = new ServiceInstaller()) + { + si.Context = new InstallContext(); + si.ServiceName = serviceInstaller1.ServiceName; + si.Uninstall(null); + } + + break; + } + } } catch { } } - private void ServiceInstaller1_BeforeUninstall(object sender, InstallEventArgs e) + protected override void OnAfterInstall(IDictionary savedState) { try { - new ServiceController(serviceInstaller1.ServiceName).Stop(); + using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName)) + { + sc.Start(); + } + } + catch + { } + } + + protected override void OnBeforeUninstall(IDictionary savedState) + { + try + { + using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName)) + { + sc.Stop(); + } } catch { }