** If you need to perform only the configuration, follow these steps:
This can be done by explicitly implementing an existing service uninstall (uninstall), and then to install a newer version. To do this, we need to update ProjectInstaller.Designer.cs, as shown below:
Consider adding the following line at the beginning of InitializeComponent (), which fires an event to delete an existing service before your current installer tries to install the service again. Here we delete the service if it already exists.
Add the following namespaces:
using System.Collections.Generic; using System.ServiceProcess;
Add the line of code below as described above:
this.BeforeInstall += new System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall);
Example:
private void InitializeComponent() { this.BeforeInstall += new System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall); this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); // // serviceProcessInstaller1 // this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; // // serviceInstaller1 // this.serviceInstaller1.Description = "This is my service name description"; this.serviceInstaller1.ServiceName = "MyServiceName"; this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; // // ProjectInstaller // this.Installers.AddRange(new System.Configuration.Install.Installer[]{ this.serviceProcessInstaller1, this.serviceInstaller1 } ); }
The following code raised by the event will then delete the service, if one exists.
void ProjectInstaller_BeforeInstall(object sender, System.Configuration.Install.InstallEventArgs e) { List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices()); foreach (ServiceController s in services) { if (s.ServiceName == this.serviceInstaller1.ServiceName) { ServiceInstaller ServiceInstallerObj = new ServiceInstaller(); ServiceInstallerObj.Context = new System.Configuration.Install.InstallContext(); ServiceInstallerObj.Context = Context; ServiceInstallerObj.ServiceName = "MyServiceName"; ServiceInstallerObj.Uninstall(null); break; } } }
PS: Along with the above changes, please consider updating the Version, ProductCode (and optional UpgradeCode) code for good practice, better version control, tracking, and maintenance
Shivanandsk
source share