mirror of
https://github.com/fergalmoran/DnsServer.git
synced 2025-12-22 09:29:50 +00:00
Windows service ProjectInstaller changes done to explicitly uninstall previously installed old version service before proceeding to install new version.
This commit is contained in:
@@ -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.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
|
||||
{ }
|
||||
|
||||
Reference in New Issue
Block a user