Windows service ProjectInstaller changes done to explicitly uninstall previously installed old version service before proceeding to install new version.

This commit is contained in:
Shreyas Zare
2018-02-16 22:41:02 +05:30
parent 3c9c760a64
commit eb1bea72f2

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install; using System.Configuration.Install;
using System.ServiceProcess; using System.ServiceProcess;
@@ -10,26 +30,58 @@ namespace DnsService
public ProjectInstaller() public ProjectInstaller()
{ {
InitializeComponent(); InitializeComponent();
serviceInstaller1.AfterInstall += ServiceInstaller1_AfterInstall;
serviceInstaller1.BeforeUninstall += ServiceInstaller1_BeforeUninstall;
} }
private void ServiceInstaller1_AfterInstall(object sender, InstallEventArgs e) protected override void OnBeforeInstall(IDictionary savedState)
{ {
try 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 catch
{ } { }
} }
private void ServiceInstaller1_BeforeUninstall(object sender, InstallEventArgs e) protected override void OnAfterInstall(IDictionary savedState)
{ {
try 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 catch
{ } { }